为什么在while循环中将此语句打印两次? [英] Why is this statement printed twice in while loop?

查看:419
本文介绍了为什么在while循环中将此语句打印两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为实践编写了这个简单的程序:

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

#define CLASSES 3
#define STUDENTS 4
int grades[CLASSES][STUDENTS];

int main(void)
{
    int i = 1;
    char t,k;
    while(i == 1)
    {
        printf("\n\n\nMENU:\nEnter the grades(E)\nReport Grades(R)\nQuit(Q)\nYour choice: ");
        k = toupper(getchar());
        printf("Input entered... %c\n", k);
        switch(k) {
            case 'E' : 
                printf("Entering the grades..\n");
                break;
            case 'R' :
                printf("Reporting the grades...\n");
                break;
            case 'Q' :
                printf("Quitting the program...\n");
                exit(0);
                break;
            default:
                printf("ERROR: %c: Incorrect menu option\n", k);
                break;
        }

    }
    return 0;
}

运行此命令时,它首先会要求我输入一个选项.如果输入"E"或"R",它将进入相应的"case"块,但是在while循环内的下一次迭代中,它不会等待我输入选择.相反,它假设我输入了"NULL",并要求我第三次输入提示.每当我输入选择项时,这种情况就会不断发生.这是该程序的输出.我在这里想念什么?

host-mb:c_practice host$ ./asd



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: E
Input entered... E
Entering the grades..



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: Input entered...

ERROR:
: Incorrect menu option



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: R
Input entered... R
Reporting the grades...



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: Input entered...

ERROR:
: Incorrect menu option



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: Q
Input entered... Q
Quitting the program...
host-mb:c_practice host$

解决方案

之所以会发生这种情况,是因为您键入了一个字母,然后按Enter键.使用另一个getchar()来吃结尾的换行符.

所以改变这个:

k = toupper(getchar());

对此:

k = toupper(getchar());
getchar(); // eat the trailing newline

当用户输入内容时,它会进入 stdin (标准输入)流,并且系统确保将用户键入的内容存储在内部缓冲区中.因此,这是您的代码所发生的事情:

因此解决方案是删除结尾的换行符


复活节彩蛋提示:

您应该收到以下信息:

warning: implicit declaration of function ‘printf’

由于缺少IO标头,因此应在主文件顶部添加以下内容:

#include <stdio.h>

类似地,您应该添加:

#include <ctype.h>  // for toupper()
#include <stdlib.h> // for exit()

另一种解决方案是使用 fgets(),有关更多信息,请参见此问题. C-scanf()vs gets()vs fgets()... >


我在scanf()上遇到了与您类似的问题,当时我在您的鞋子里,所以我写下了

When I run this, it first asks me to enter a choice. If I enter 'E' or 'R', it goes into the respective 'case' block but in the next iteration within the while loop, it doesn't wait for me to enter my choice. Instead it assumes I entered "NULL" and asks for my prompt third time. This keeps happening everytime I enter a choice. Here is the output of this program. What am I missing here?

host-mb:c_practice host$ ./asd



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: E
Input entered... E
Entering the grades..



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: Input entered...

ERROR:
: Incorrect menu option



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: R
Input entered... R
Reporting the grades...



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: Input entered...

ERROR:
: Incorrect menu option



MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: Q
Input entered... Q
Quitting the program...
host-mb:c_practice host$

This happens because you type a letter and then you press enter. Use another getchar() to eat the trailing newline.

So change this:

k = toupper(getchar());

to this:

k = toupper(getchar());
getchar(); // eat the trailing newline

When the user inputs something, it goes to the stdin (standard input) stream and the system makes sure to store what the user typed in a internal buffer. So here is what happened with your code:

So the solution is to eat the trailing newline!


Easter eggs tips:

You should receive this:

warning: implicit declaration of function ‘printf’

because you lack of the IO header, thus you should add in the top of your main file this:

#include <stdio.h>

Similarly you should add:

#include <ctype.h>  // for toupper()
#include <stdlib.h> // for exit()

Another solution would be to use fgets(), see this question for more C - scanf() vs gets() vs fgets().


I had a similar issue to yours with scanf() and I was in your shoes, so I had written down the solution at the time.

这篇关于为什么在while循环中将此语句打印两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆