为什么 atoi 函数不能使用解除引用的指针作为参数?

问题描述

#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;

int addition(const char*);

int main(){
    int sum;
    char str[100];
    cout<<"Enter a string composed of only numbers.\t";
    cin.getline(str,sizeof(str));
    sum=addition(str);
    cout<<"The sum of digits is "<<sum<<endl;
    return 0;
}
int addition(const char* strPtr){
    int total=0;
    int i=0;
    while(strPtr[i]!='\0'){
        total+=atoi(strPtr[i]);
        i++;
    }
    return total;
}

我正在尝试查找用户输入字符串中的数字总和,但 atoi 函数不起作用。我做错了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)