Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

c - Why is this statement printed twice in while loop?

I wrote this simple program for practise:

#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("


MENU:
Enter the grades(E)
Report Grades(R)
Quit(Q)
Your choice: ");
        k = toupper(getchar());
        printf("Input entered... %c
", k);
        switch(k) {
            case 'E' : 
                printf("Entering the grades..
");
                break;
            case 'R' :
                printf("Reporting the grades...
");
                break;
            case 'Q' :
                printf("Quitting the program...
");
                exit(0);
                break;
            default:
                printf("ERROR: %c: Incorrect menu option
", k);
                break;
        }

    }
    return 0;
}

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$
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...