我正在从键盘读取数字,然后我必须单独操作每个数字(它是一个八进制到十进制转换器)。 是否有类似于Java的charAt()方法,可以用来处理特定的数字?在字符串中定义的位置获取字符C在C
目前,我有下面的代码(不完全),但是在编译的时候,它会返回“错误:下标值既不是数组,也不指针”
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
printf("Please enter an octal number ending with #");
char nextNum = getchar();
char number;
int counterUp = 0; //Records how many digits are entered
int counterDown = 1; //Records progress during conversion
int decimalNumber = 0;
while(nextNum != '#') //reads in the whole number, putting the characters together to form one Octal number.
{
number = (number + nextNum);
counterUp++;
nextNum = getchar();
}
//Begin converson from Octal to Decimal
while(counterUp >= 0)
{
int added = (number[counterUp] * (pow(8, counterDown)));
decimalNumber = (decimalNumber + added);
counterDown++;
}
}
我不希望被告知如何从八进制去到小数点,就是如何一次处理一位数字。
请发布完整的编译器错误消息,并指出代码中编译器标记为不正确的行。 – 2012-03-27 01:50:29
不正确的行是'int added =(number [counterUp] *(pow(8,counterDown)));' – Crashworks 2012-03-27 01:51:33
由于'number'是一个单一的字符,因此它就像是一个数组类型一样访问没有任何意义...使用如下建议的字符数组。 – prelic 2012-03-27 01:55:37