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中的while循环突然终止_C_Loops_Input - Fatal编程技术网

为什么C中的while循环突然终止

为什么C中的while循环突然终止,c,loops,input,C,Loops,Input,只是测试一些代码;在输入“n”之前,应运行以下命令。但一轮后就停止了。谁能解释一下,帮我实现我想要的 #include <stdio.h> int main () { char another = 'y'; int num; while ( another == 'y' ) { printf ("Enter an number "); scanf ("%d", &num); printf ("square

只是测试一些代码;在输入“n”之前,应运行以下命令。但一轮后就停止了。谁能解释一下,帮我实现我想要的

#include <stdio.h>
int main ()
{
  char another = 'y';
  int num;

  while ( another == 'y' )
    {
        printf ("Enter an number ");
        scanf ("%d", &num);
        printf ("square of %d is %d", num, num * num);         
        printf ("\nWant to enter another number y/n\n");
        scanf ("%c", &another);

    }

}
#包括
int main()
{
char-other='y';
int-num;
而(另一个=='y')
{
printf(“输入一个数字”);
scanf(“%d”和&num);
printf(“%d的平方是%d”,num,num*num);
printf(“\n希望输入另一个数字y/n\n”);
scanf(“%c”和另一个);
}
}
谢谢

我真的很感谢大家的评论。我确实在网上搜索了GDB,后来使用了它。我不得不说,这使得识别问题变得容易多了。
非常感谢

%c

scanf (" %c", &another);
在上一个
scanf()
之后使用缓冲区中保留的换行符


1) 使用
main()的标准定义

2) 务必检查scanf()和其他函数的返回值,以确保读取的值没有任何错误。

使用

scanf (" %c", &another);
       ^^^^^
而且最好是写

printf ("square of %d is %lld", num, ( long long int )num * num);
                         ^^^^        ^^^^^^^^^^^^^^^^^^^^        

请看。在发布之前,您是否想过使用调试器检查scanf读取的内容,甚至只是打印出其值?谢谢您的回答,但不会让我觉得我什么都不知道@用户3326293完全没有。我们初学者应该互相帮助。:)非常感谢。@user3326293,不客气。:)
printf ("square of %d is %lld", num, ( long long int )num * num);
                         ^^^^        ^^^^^^^^^^^^^^^^^^^^