简体   繁体   中英

How to put EOF(end of file ) in code::blocks?

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int grade,
        aCount = 0,
        bCount = 0,
        cCount = 0,
        dCount = 0,
        fCount = 0;

    cout << "Enter the letter grades." << endl
        << "Enter the EOF character to end input." << 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 '\t':
            case ' ':
                break;

                    default:
                        cout << "Incorrect letter grade entered."
                            << " Enter a new grade." << endl;
                        break;

        }
    }    

    cout << "\n\nTotal for each letter grade are:"
         << "\nA: " << aCount
         << "\nB: " << bCount
         << "\nC: " << cCount
         << "\nD: " << dCount
         << "\nF: " << fCount << endl;

                return 0;
}    

I build and run this and it says "EOF has not been declared" at line 19 ie the line with the while header. I typed the exact codes one by one.

The EOF macro is defined in the cstdio , but it's usually better to use C++-style IO than C-style IO. Instead of checking the return value to see if it's EOF , it's better to check cin.eofbit (or, since other problems are possible, the cin.failbit ). You can check the failbit of a stream by trying to evaluate it as a bool , as below.

If you replace your while loop with a for loop, as

for (char grade = cin.get(); cin; grade = cin.get()) {

it should work better (in this case you should also get rid of the initial grade declaration, since it is only used in the loop and should be declared there).

EDIT:

This can be simplified further. The get method supports passing in a character reference, so you can call cin.get(grade) instead of grade = cin.get() . The advantage of this is that, when passed a character reference, get returns a copy of the stream, so you can do the read and check the state at the same time. Using this method, you need to declare grade in advance, but the loop can start with

while (cin.get(grade)) {
while ( ( grade = cin.get() ) != EOF ) {

In the above line you are only taking the grade as input. There should be a cin.get() for EOF also,unless it will not work. When you are doing this you need to declare EOF first as char, as in the above line the comparison is in between two characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM