Compiler errors 在执行代码块时,其跳过字符输入,即scanf()

Compiler errors 在执行代码块时,其跳过字符输入,即scanf(),compiler-errors,char,syntax-error,runtime,scanf,Compiler Errors,Char,Syntax Error,Runtime,Scanf,每次我尝试运行代码时,字符输入语句scanf(“%c”,&ch);完全忽略并跳转到netx行,导致无限while循环的executioon(我在上面的代码块中使用了注释行)。我找不到哪里出错了。不过,其他功能工作正常 void main() { int n,g,i; char ch; printf("Enter the number of students present today\n"); scanf("%d",&n); for(

每次我尝试运行代码时,字符输入语句scanf(“%c”,&ch);完全忽略并跳转到netx行,导致无限while循环的executioon(我在上面的代码块中使用了注释行)。我找不到哪里出错了。不过,其他功能工作正常

void main()
 {
     int n,g,i;
     char ch;

     printf("Enter the number of students present today\n");
     scanf("%d",&n);
     for(i=0;i<n;i++)
     {
        printf("Enter the rolls of the students who are present\n");
        attendance();
     }

     printf("Enter the state of the student entry/departure\n");
     printf("\n a for Entry and d for departure and c for door close\n");
     scanf("%c",&ch);


     /*while(1)
     {
         switch(ch)
         {
             case 'a':
            printf("Enter the roll of the students who arrived\n");
            entry();
            break;

            case 'd':
            printf("Enter the roll of the student who departed\n");
            departure();
            break;

            case 'c':
            exit(0);

            default:
            printf("Wrong\n");

         }
     }*/
     printf("Want to see the rolls of the student left in class?[1/0]");
     scanf("%d",&g);
     if(g==1)
     display();
 }


void main()
{
int n,g,i;
char ch;
printf(“输入今天出席的学生人数\n”);
scanf(“%d”和“&n”);

对于(i=0;i是的,
scanf
可能很棘手。要调试此类问题,在
scanf
后面插入一行可以帮助您准确显示结果,例如:

    printf("scanf got character '%c' (0x%02x)\n", ch, ch);
这将向您显示该字符的字符和数字值,如果该字符是不易识别的打印外观的外来字符,这将非常有用

如果您在本例中这样做,它将告诉您
scanf(“%c”)
为您提供了它找到的下一个字符,这是一个换行符。这是仍然位于输入缓冲区中的换行符,是上一个
scanf
调用您的
考勤
函数检查的输入行末尾留下的

如果希望
scanf
跳过过去的剩余空白字符,包括换行符,则应在
scanf
格式字符串的开头添加空格。因此,请更改现有的:

    scanf("%c",&ch);

这会让你得到你想要的行为

    scanf(" %c",&ch);