Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 一直读到我输入了一个整数_C_Integer_Scanf - Fatal编程技术网

C 一直读到我输入了一个整数

C 一直读到我输入了一个整数,c,integer,scanf,C,Integer,Scanf,我是C语言的新手,我曾经在Python中工作过,我想看看我读到的东西是不是整数。如果没有,请阅读,直到我设法输入一个数字 我做了一些研究,发现如果读取正确,函数scanf实际上返回1,否则返回0。 所以,我已经写了这段代码,我不明白为什么这是一个无限循环,在控制台上写“给出一个整数” #include <stdio.h> int main() { int a; int b = 1; do { printf("Give an integer\n

我是C语言的新手,我曾经在Python中工作过,我想看看我读到的东西是不是整数。如果没有,请阅读,直到我设法输入一个数字

我做了一些研究,发现如果读取正确,函数
scanf
实际上返回1,否则返回0。 所以,我已经写了这段代码,我不明白为什么这是一个无限循环,在控制台上写“给出一个整数”

#include <stdio.h>

int main() {
    int a;
    int b = 1;
    do {
        printf("Give an integer\n");
        b = scanf("%d", &a);
    } while (b == 0);
}
#包括
int main(){
INTA;
int b=1;
做{
printf(“给出一个整数\n”);
b=扫描频率(“%d”和&a);
}而(b==0);
}

scanf()的问题是,对于大多数说明符(如
%d”
)来说,当遇到第一个空格时,它会停止读取,因此它会留在输入中,这就是为什么如果不丢弃此类空格,再次读取会导致问题,因为它会在下次调用时立即返回。按Enter键或Return键时会引入一个强制空格,即
'\n'
新行字符(或换行符)

如果您想读取一个整数,并确保您读取了,您可以这样尝试

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int
main(void)
{
    long int value; // You can adapt this to use `int'
                    // but be careful with overflow
    char line[100];
    while (fgets(line, sizeof(line), stdin) != NULL) {
        char *endptr;
        value = strtol(line, &endptr, 10);
        if ((isspace(*endptr) != 0) && (endptr != line))
            break;
    }
    fprintf(stdout, "%ld\n", value);
    return 0;
}
#包括
#包括
#包括
int
主(空)
{
long int value;//您可以调整它以使用'int'
//但是要小心溢出
字符行[100];
while(fgets(line,sizeof(line),stdin)!=NULL){
char*endptr;
值=strtol(第行和第行,10);
如果((isspace(*endptr)!=0)和&(endptr!=line))
打破
}
fprintf(标准输出,“%ld\n”,值);
返回0;
}
阅读用户手册以了解此代码

您可以按照注释中的建议,从输入中删除空白字符,但这更难,而且您在使用
scanf()
时仍会遇到其他问题,例如处理非直接输入的空输入

我不明白为什么这是一个无限循环,在控制台上写“giveanintiger”

#include <stdio.h>

int main() {
    int a;
    int b = 1;
    do {
        printf("Give an integer\n");
        b = scanf("%d", &a);
    } while (b == 0);
}

问题在于
scanf()
不使用无法与指定格式匹配的数据。它使这些字符在输入流中不可读。因此,如果您尝试以相同的格式从相同的流中再次读取,而不通过其他方式使用至少一个字符,则可以确定输入将再次不匹配。一次又一次。一次又一次

为了避免无限循环,您需要在每次匹配失败后使用至少一个非匹配输入字符。有很多方法可以做到这一点;这里有一个相当简单的例子:

#include <stdio.h>

int main() {
    int a;

    do {
        printf("Give an intiger\n");
        if (scanf("%d", &a)) {
            // breaks from the loop on a successful match or an error
            break;
        }
        // consume the remainder of one line of input without storing it
        if (scanf("%*[^\n]") == EOF) {
            break;
        }
    } while (1);
}

由于
&&
运算符短路,仅当第一个
scanf()
调用返回零时,才会执行第二个
scanf()
调用,并且循环将在第一次迭代后退出,其中第一个
scanf()
调用返回非零或第二个
EOF
(指示错误).

问题在于
scanf()
不会使用与字段描述符不匹配的数据。因此,当您返回重试时,相同的不匹配数据仍在等待处理。这是有效的!但我真的不明白为什么:))说
scanf()
忽略空白字符充其量只是误导,至于OP使用
scanf()
的真实性,这并不能解释他所描述的问题。@JohnBollinger你完全正确!我修复了它,我认为现在更容易理解OP代码的问题。
(isspace(*endptr)!=0)
允许输入有效的
“123 456”
。也许
(*endptr='\n'|*endptr='\0')
?在scanf之后添加getchar()也可以正常工作。但是为什么scanf函数不使用特定格式的数据呢?这是一个bug还是一个语言设计决策?@DANIROLST,在失败的
scanf()
之后添加
getchar()
,将避免无限循环,但它仍然会为某些输入产生令人惊讶的行为。例如,尝试使用“abc123”。@DANIROLST,C标准明确规定了
scanf()
行为的这一方面。而且,这是有道理的:如果
scanf()
使用了不匹配的数据,那么该数据将丢失,并且没有机制将其取回。此外,如果
scanf()
想要消耗一些不匹配的输入,那么您认为它应该消耗多少?没有一个答案适用于所有情况。@DANIROLST:有许多文本处理应用程序,您希望保留不匹配的输入,然后用不同的格式重试,而不是丢弃它。噢,现在这是有意义的。我问这个问题是因为我从来没有遇到过这样的情况,当我应该使用一些用户没有正确给出的数据时,我认为信息应该丢失。