Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_Loops_While Loop_Scanf_Do While - Fatal编程技术网

C 循环将永远持续下去

C 循环将永远持续下去,c,loops,while-loop,scanf,do-while,C,Loops,While Loop,Scanf,Do While,下面的代码按照预期编译和工作,尽管程序流中有一个我不理解的令人沮丧的错误。 如果我通过2或5作为输入,则main函数中间的循环工作正常。但是,当我通过-3或任何低于零的值(例如返回-1的字符)时,循环将永远继续,程序甚至不会暂停为scanf函数提供输入 #include <stdio.h> #include <stdlib.h> void getNum(char * prompt, int*num) { printf("%s", prompt); sca

下面的代码按照预期编译和工作,尽管程序流中有一个我不理解的令人沮丧的错误。
如果我通过2或5作为输入,则main函数中间的循环工作正常。但是,当我通过-3或任何低于零的值(例如返回-1的字符)时,循环将永远继续,程序甚至不会暂停为scanf函数提供输入

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

void getNum(char * prompt, int*num)
{
    printf("%s", prompt);
    scanf("%d", num);
}

int main(int argc, char ** argv)
{
    int num = -1;
    while(num < 0) { // problem here
        getNum("Number of times you go to the gym in a week: ", &num);
    }
    return EXIT_SUCCESS;
}
#包括
#包括
void getNum(char*prompt,int*num)
{
printf(“%s”,提示符);
scanf(“%d”,num);
}
int main(int argc,字符**argv)
{
int num=-1;
而(num<0){//这里有个问题
getNum(“一周内你去健身房的次数:,&num);
}
返回退出成功;
}
我想知道是不是错了

我注意到一件奇怪的事。。当我将循环更改为do while循环时,它工作得很好

int main(int argc, char ** argv)
{
    int num;
    do {
        getNum("Number of times you go to the gym in a week: ", &num);
    } while (num < 0); // this works fine ..
    return EXIT_SUCCESS;
}
int main(int argc,char**argv)
{
int-num;
做{
getNum(“一周内你去健身房的次数:,&num);
}while(num<0);//这很好。。
返回退出成功;
}
另外,出于某种原因,我重新编译了代码,它工作得很好


有人能解释一下吗?

您可以在尝试
scanf
后尝试清除STDIN数据:

void getNum(char * prompt, int*num)
{
    printf("%s", prompt);
    scanf("%d", num);

    // clean stdin
    char c;
    scanf("%c",&c);
    while (c != '\n' && c != EOF) scanf("%c",&c);
}

您可以在尝试
scanf
后尝试清除STDIN数据:

void getNum(char * prompt, int*num)
{
    printf("%s", prompt);
    scanf("%d", num);

    // clean stdin
    char c;
    scanf("%c",&c);
    while (c != '\n' && c != EOF) scanf("%c",&c);
}

您可以在尝试
scanf
后尝试清除STDIN数据:

void getNum(char * prompt, int*num)
{
    printf("%s", prompt);
    scanf("%d", num);

    // clean stdin
    char c;
    scanf("%c",&c);
    while (c != '\n' && c != EOF) scanf("%c",&c);
}

您可以在尝试
scanf
后尝试清除STDIN数据:

void getNum(char * prompt, int*num)
{
    printf("%s", prompt);
    scanf("%d", num);

    // clean stdin
    char c;
    scanf("%c",&c);
    while (c != '\n' && c != EOF) scanf("%c",&c);
}
接受回答后

scanf(“%d”,num)
,在读取非数字输入时,simple返回0,仅保留
*num
。有问题的文本仍在stdin中,后续调用将得到相同的文本和结果。代码应检查scanf()结果值

// weak
scanf("%d", num); // fails to consume offending input.

// good
*num = 0; // default answer
int retval;
do {
  printf("%s", prompt);
  retval = scanf("%d", num);  // returns EOF, 0, or 1
  // consume rest of line
  int c;
  while ((c = fgetc(stdin)) != '\n' && c != EOF);    
} while (retval == 0);  // repeat is no number read and stdin still open

[编辑]

避免使用
scanf()
。提供一个解决方案,在接受答案后,很好地处理阅读
int

scanf(“%d”,num)
,在读取非数字输入时,simple返回0,仅保留
*num
。有问题的文本仍在stdin中,后续调用将得到相同的文本和结果。代码应检查scanf()结果值

// weak
scanf("%d", num); // fails to consume offending input.

// good
*num = 0; // default answer
int retval;
do {
  printf("%s", prompt);
  retval = scanf("%d", num);  // returns EOF, 0, or 1
  // consume rest of line
  int c;
  while ((c = fgetc(stdin)) != '\n' && c != EOF);    
} while (retval == 0);  // repeat is no number read and stdin still open

[编辑]

避免使用
scanf()
。提供一个解决方案,在接受答案后,很好地处理阅读
int

scanf(“%d”,num)
,在读取非数字输入时,simple返回0,仅保留
*num
。有问题的文本仍在stdin中,后续调用将得到相同的文本和结果。代码应检查scanf()结果值

// weak
scanf("%d", num); // fails to consume offending input.

// good
*num = 0; // default answer
int retval;
do {
  printf("%s", prompt);
  retval = scanf("%d", num);  // returns EOF, 0, or 1
  // consume rest of line
  int c;
  while ((c = fgetc(stdin)) != '\n' && c != EOF);    
} while (retval == 0);  // repeat is no number read and stdin still open

[编辑]

避免使用
scanf()
。提供一个解决方案,在接受答案后,很好地处理阅读
int

scanf(“%d”,num)
,在读取非数字输入时,simple返回0,仅保留
*num
。有问题的文本仍在stdin中,后续调用将得到相同的文本和结果。代码应检查scanf()结果值

// weak
scanf("%d", num); // fails to consume offending input.

// good
*num = 0; // default answer
int retval;
do {
  printf("%s", prompt);
  retval = scanf("%d", num);  // returns EOF, 0, or 1
  // consume rest of line
  int c;
  while ((c = fgetc(stdin)) != '\n' && c != EOF);    
} while (retval == 0);  // repeat is no number read and stdin still open

[编辑]



避免使用
scanf()
。提供一个解决方案来很好地处理读取
int

您的代码在ideone()上运行良好。@dasblinkenlight:由于某些原因,它在我的机器上不工作。尝试添加
scanf
的错误处理,看看它是否返回任何错误。请注意,这两种情况下的测试条件不同。这可能就是你有不同行为的原因。@downhillFromHere:哦,那只是一个打字错误……你的代码在ideone()上运行得很好。@dasblinkenlight:出于某种原因,它在我的机器上不起作用。.尝试添加
scanf
的错误处理,看看它是否返回任何错误。请注意,这两种情况下的测试条件是不同的。这可能就是你有不同行为的原因。@downhillFromHere:哦,那只是一个打字错误……你的代码在ideone()上运行得很好。@dasblinkenlight:出于某种原因,它在我的机器上不起作用。.尝试添加
scanf
的错误处理,看看它是否返回任何错误。请注意,这两种情况下的测试条件是不同的。这可能就是你有不同行为的原因。@downhillFromHere:哦,那只是一个打字错误……你的代码在ideone()上运行得很好。@dasblinkenlight:出于某种原因,它在我的机器上不起作用。.尝试添加
scanf
的错误处理,看看它是否返回任何错误。请注意,这两种情况下的测试条件是不同的。也许这就是为什么你会有不同的行为。@downhillFromHere:哦,那只是一个打字错误……我想这是有道理的。。我能不能就这么简单?+1,但我建议
intc;而((c=fgetc(stdin))!='\n'&&c!=EOF)清除STDIN此解决方案失败。当
scanf(“%c”和&c)时并关闭
stdin
c
未获取值
EOF
,该值可能不适合
char
。在
c
中没有放入任何内容!它包含随机数据@Alter Mann是正确的,使用
int c;而((c=fgetc(stdin))!='\n'&&c!=EOF)
刷新
stdin
至行尾。这个答案也不能保证
*num
有有效的数据。我想这是有道理的。。我能不能就这么简单?+1,但我建议
intc;而((c=fgetc(stdin))!='\n'&&c!=EOF)清除STDIN此解决方案失败。当
scanf(“%c”和&c)时并关闭
stdin
c
未获取值
EOF
,该值可能不适合
char
。在
c
中没有放入任何内容!它包含随机数据@Alter Mann是正确的,使用
int c;而((c=fgetc(stdin))!='\n'&&c!=EOF)
刷新
stdin
至行尾。这个答案也不能保证
*num
有有效的数据。我想这是有道理的。。我不能就这样吗?+1,但是