Ho scritto questo semplice programma per la pratica:Perché questa frase è stampata due volte nel ciclo 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;
}
Quando ho eseguito questo, prima mi chiede di entrare in una scelta. Se inserisco 'E' o 'R', va nel rispettivo blocco 'caso' ma nella prossima iterazione all'interno del ciclo while, non aspetta che entri nella mia scelta. Invece presuppone che io abbia inserito "NULL" e chiedo il mio prompt per la terza volta. Questo continua a succedere ogni volta che entro in una scelta. Ecco l'output di questo programma. Cosa mi manca qui?
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$
Perché si è premuto e, quindi premuto invio. Sono due personaggi. – immibis
'k = touper (getchar());' -> 'k = touper (getchar()); getchar();' uno per newline. – BLUEPIXY