|
From: Ioannis V. <no...@ya...> - 2000-11-11 18:32:26
|
At first, you did not include <stdio.h>. Second, the ==-1 also does not work
since getchar reads a character a time and the -1 of the user input is not
read together. Now the most important, the program reads the character, the
error message is for the '\n' character which the next getchar() reads after
your input. So take a look at the improved code:
#include <stdio.h> /* You forgot it */
#include <stdlib.h>
int main()
{
int grade;
int aCount = 0, bCount = 0;
printf("Enter the letter grades.\n");
printf("Enter the Q to end input.\n");
while( (grade = getchar()) != 'Q') /* 'Q' instead of '-' '1' */
{
getchar(); /* It absorbs the next '\n' */
switch(grade)
{
case 'a': case 'A':
++aCount;
break;
case 'b': case 'B':
++bCount;
break;
default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}
printf("%d\n", aCount);
printf("%d\n", bCount);
system("PAUSE");
return 0;
}
|