default in switch calling itself when it shouldnt

closed account (E3h7X9L8)
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
WHat is your input?
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.
closed account (E3h7X9L8)
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;
why not use formatted input instead?
Topic archived. No new replies allowed.