I'm a beginner and I copied this code from a book but it doesn't work because when I enter EOF, the program instead of quitting the while loop prints the default message twice. It is like a hour that i search for a solution but nothing that i've tried works, thanks for help. the program is supposed to count the number of each kind of grades.
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <math.h>
#include <ctype.h>
using namespace std;
int main()
{
int grade,
aCount = 0,
bCount = 0,
cCount = 0,
dCount = 0,
fCount = 0;
cout << "Enter the letter grades (EOF to quit) ->>";
while ((grade = cin.get()) != 'EOF') {
switch (grade) {
case 'A':
++aCount;
break;
case 'B':
++bCount;
break;
case 'C':
++cCount;
break;
case 'D':
++dCount;
break;
case 'F':
++fCount;
break;
case '\n':
case '\t':
case ' ':
break;
default :
cout << "Invalid letter grade entered. Enter a new grade." << endl;
break;
}
}