0

我已经阅读了其他帖子,这些帖子得到了“从整数不进行强制转换”错误,但我对它的确切含义感到困惑。如果我不强制转换(强制转换的意思是说)),是不是说函数默认gets使 int 变量成为指针?inputgets ((int) input

int directions() 
{   
    int input;

    printf("Type '1', '2', etc to see a problem or type '(add an 'all' option to see a     list of all the problems)\n");
    gets( input);

    switch ( input) {
        case 1: euler1();
        case 2: euler2();
    }

    return input;
}

int main()
{   
    printf("Welcome to Project Euler's Problems in C!\n");
    directions();

    return 0;
}
4

3 回答 3

1

第一个参数gets()是 a char*,一个指向缓冲区的指针。它不返回int. 如果您查看有关 的一些文档gets,您将了解它是如何正确使用的。

读入字符串后,您需要使用atoi它从 ASCII 转换为整数。

请注意,您最好使用类似的方法来扫描输入流以获取所需的输入。fscanf(stdin, "%d", &input);

于 2012-09-22T01:00:31.393 回答
0

to的第一个参数gets()char*always(指向缓冲区的指针)。它不返回 int 或其他数据类型。

使用类似的东西fscanf(stdin, "%d", &input);来扫描输入流以获取所需的输入。

gets()尽量少用,用fgets

很高兴编码:)

于 2018-10-29T15:41:48.703 回答
-1

gets()没有得到 int。使用atoi()进行转换。

char *s;
s = gets(buff)
input = atoi(s);
于 2012-09-22T01:08:41.040 回答