2

I am using Windows 7 Ultimate. I am new to C++. Following is my exercise for switch statement.

void GradeBook::inputGrades()
{
    int grade;

    cout << "Enter Grade: " << endl;

    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;

           case '\n':
           case ' ':
           case '\t':
               break;

            default:
                cout << "Incorrect data. Re Enter" << endl;
                break;
        }

    }
}

I run this inside netbeans, and I pressed all the combinations ctrl+c , ctrl+z, ctrl+d but it is not ending!! Why is that? Have I done something wrong? Please help!!

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • I think your question isn't really about EOF, but here's about it : http://en.wikipedia.org/wiki/End-of-file – slaphappy Aug 13 '12 at 15:57
  • 1
    Control+Z or F6. Normally needs to be entered on a line by itself, so basically enter followed by Control+Z or F6. – Jerry Coffin Aug 13 '12 at 16:03
  • hmm..Still not working.. Are you sure it is working in netbeans? Or else, isn't it better to change the argument test to -1 ? So, when the user enter -1, it will break. – PeakGen Aug 13 '12 at 16:30

2 Answers2

2

cin.get() is pretty low level. The code should use a higher-level interface. It's supposed to read a character at a time, so write it that way:

char grade;
while (cin >> grade)

The stream extractor will fail at end of file, and that will make the while loop terminate.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
2

An EOF character is Ctrl+Z followed by a newline character on Windows platforms.

Presumably that will be the same for the console within Netbeans.

Community
  • 1
  • 1
sacko87
  • 143
  • 1
  • 6