我正在尝试从用户输入一个数字(如12345
)并将其转换为 int。我正在使用的代码是:
int convertToNumber(char a[10]) {
int output = 0;
int b;
int intArray[10];
//Finds length
for (int i = 0; a[i]!=0; i++) {
if (a[i]==0) {
b=i-1;
}
}
//Runs through every letter.
for (int i = 0; a[i]!=0; i++) {
//Checks if user inputted anything but letter
intArray[i] = a[i] - '0';
//Multiplying it by the distance from the end
intArray[i]= intArray[i] * (10^(b-i));
//Adds to output
output=+intArray[i];
}
return output;
}
但是,这最终不会像我希望的那样。有谁知道怎么了?