C - 执行循环 "enter more (y/n)" - 字符问题



所以我正在学习基本的C技能,我想设计一个可以输入用户想要的尽可能多的数字的代码。然后,它应该显示正、负&输入0个整数。我搜索了谷歌& &;StackOverflow。根据这些程序,代码看起来很好。它编译&运行。但是每当我在"enter more?"Y/n",它返回到代码..请看下面的代码:

#include<stdio.h>
int main() 
{
    int no,count_neg=0,count_pos=0,count_zero=0;
    char ch='y';
    clrscr();
    do
    {
        puts("Enter number");
        scanf("%d",&no);
        if (no>0)
            count_pos++;
        else if (no<0)
            count_neg++;
        else
            count_zero++;
        puts("want more? - y/n ");
        scanf("%c",&ch);
    }
    while (ch=='y');
    if (ch=='n')
    {
        printf("No of positives = %d",count_pos);
        printf("No of negatives = %d",count_neg);
        printf("No of zeros = %d",count_zero);
    }
    getch();
    return 0;
}

问题在于"scanf("%c", &ch);"

实际情况是:
假设您输入'y'作为选项,然后按'enter'(回车),回车是一个字符和
它的字符值是10(因为它是一个新的行字符),因此scanf接受'return'因为它的输入和继续。

解决方案:1. 在scanf()之前使用getchar()

//你的代码

  getchar();  
  scanf();  

//你的代码Getchar()将返回值作为其输入,因此您将得到实际值。

  1. 添加'n'到scnaf()
    //代码
    scanf(" n % c",和ch);
    //代码

当scanf()遇到'n'字符时,它会跳过它(谷歌scanf,了解如何
以及为什么),因此将预期值存储在'ch'中。

一个"更好"的形式:

int main()

:

int main(void)

clrscr not standard C.

您应该检查任何可能指示"有趣状态"的函数的返回值,例如失败条件,并且您可以从中优雅地处理这种情况。在本例中,scanf就是这样一个函数。

我相信你的第一个做…while条件将变为false,因为它将拾取第一个scanf调用后的换行字符。您可能希望阅读有关getchargetc的内容,而不是使用scanf来检查是否再次运行循环。可以"吃掉"不需要的字符,包括换行符

在这里,我已经纠正了这个问题。问题在于,您在每个数字之后按下的"enter"是一个字符,并且被scanf()占用,因为它在那里扫描某些字符。因此,我在scanf();之前添加了getchar();,因此"enter"由getchar();占用,并且scanf()现在可以自由地获取您的输入。

 #include<stdio.h>
 int main() 
  {
      int no,count_neg=0,count_pos=0,count_zero=0;
      char ch='y';
      clrscr();
      do
       {
         puts("Enter number");
         scanf("%d",&no);
         if(no>0)
         count_pos++;
         else if(no<0)
         count_neg++;
         else
         count_zero++;
         puts("want more? - y/n ");
         getchar();//<---- add this here 
         scanf("%c",&ch);
        }
        while(ch=='y');
    if(ch=='n')
    {
     printf("No of positives = %d",count_pos);
     printf("No of negatives = %d",count_neg);
     printf("No of zeros = %d",count_zero);
    }
    getch();
    return 0;
 }

要修复输入,可以使用像这样的C字符串scanf("%s",…);

如果您输入多个字符,这可能会中断,因为scanf将继续读取,直到用户点击enter,而您的ch变量只有一个字符的空间。

我在在线编译器中运行您的代码。我不确定其他编译器。

我稍微修改了你的代码。也就是说,我先读char,然后读int。如果不改变顺序,char变量保存int变量值。这就是为什么ch变量没有保存变量的值。

 #include<stdio.h>
 int main() 
 {
  int no,count_neg=0,count_pos=0,count_zero=0;
  char ch='y';
  do
   {
     puts("Enter number");
     scanf("%c",&ch);
     scanf("%d",&no);
     if(no>0)
     count_pos++;
     else if(no<0)
     count_neg++;
     else
     count_zero++;
     puts("want more? - y/n ");        
    }
    while(ch=='y');
    if(ch=='n')
    {
     printf("No of positives = %d",count_pos);
     printf("No of negatives = %d",count_neg);
     printf("No of zeros = %d",count_zero);
    }
    return 0;
    }

编辑:每当通过键盘读取整数和字符时。它存储int值并输入键值。这就是原因。

你必须加上

scanf("%d",&no);
you code
......
.....
fflush(stdin);
scanf("%c",&ch);

use:

ch = getche();

代替:

scanf("%c", &ch);

最新更新