Sep 7, 2015 at 6:22pm UTC
why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
void GradeBook::inputGrades()
{
int grade;
cout << "Enter grades (ctrl+z to quit) " ;
while ((grade = cin.get()) != EOF)
{
switch (grade)
{
case 'A' :
case 'a' :
aCount++;
break ;
case 'B' :
case 'b' :
bCount++;
break ;
case 'C' :
case 'c' :
cCount++;
break ;
case 'D' :
case 'd' :
dCount++;
break ;
case 'F' :
case 'f' :
fCount++;
break ;
default :
cout << "\nGrade entered is invalid!\n" ;
cout << "Enter a new one\n" ;
break ;
}
}
}
Last edited on Sep 7, 2015 at 6:22pm UTC
Sep 7, 2015 at 6:29pm UTC
Are you sure it reads the value that you are expecting? Print the value of grade on line 6 to make sure.
Note that get() reads unformatted input . It does not ignore whitespace characters.
Sep 7, 2015 at 6:33pm UTC
the problem was that everytime after I input a character and pressed enter a newline was entered too which was filtered by default label. I solved this by making a special case for the newline case '\n' : break ;
Sep 7, 2015 at 6:38pm UTC
why not use formatted input instead?