简体   繁体   中英

C++ while loop reading input

I submitted this program and it works perfectly, but my teacher says that there is something wrong with my while loop even though I am getting the correct answers. Any tips or help?

What happens in the while loop when the end of file is reached and the read on line 43 becomes invalid? The way your program is structured you do not see the problem, but it is there. Should restructure the while loop to account for this.

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    ifstream inputfile;

    char choice;


    int NumberOfIntegers = 0,
        SumOfIntegers = 0,
        Average = 0 ,
        LargestValue,
        SmallestValue,
        integer;

    inputfile.open("random.txt");

    if(!inputfile)
    {
        cout << "the file could not be open" << endl;
    }


    inputfile >> integer;

    //initialize smallest and largest
    SmallestValue = integer;
    LargestValue = integer;

    while(inputfile)
    {
        NumberOfIntegers++;
        SumOfIntegers = SumOfIntegers + integer;

        inputfile >> integer;
        if( integer > LargestValue || integer < SmallestValue)
        {

            if ( integer > LargestValue)
                LargestValue = integer;

            else 
                SmallestValue = integer;
        }
    }

    if(NumberOfIntegers > 0 )
    {
        Average = SumOfIntegers / NumberOfIntegers;
    }

    //closing input file
    inputfile.close();

    do
    {
        //Display Menu
        cout << "Make a selection from the list" << endl;
        cout << "A.   Get the largest Value" << endl;
        cout << "B.   Get the smallest Value" << endl;
        cout << "C.   Get the sum of the values" << endl;
        cout << "D.   Get the average of the values" << endl;
        cout << "E.   Get the number of values entered" << endl;
        cout << "F.   End this program" << endl << endl;

        cout << "Enter your choice -->  ";
        cin >> choice;

        cout << endl;

        switch (choice)
        {
        case 'a':
        case 'A': cout << "The largest value is " << LargestValue << endl;
            break;

        case 'b':
        case 'B': cout << "The smallest value is " << SmallestValue << endl;
            break;

        case 'c':
        case 'C': cout << "The sum of the values entered is " << SumOfIntegers << endl;
            break;

        case 'd':
        case 'D': cout << "The average of the values entered is " << Average << endl;
            break;

        case 'e':
        case 'E': cout << "The number of values entered is " << NumberOfIntegers << endl;
            break;

        case 'f':
        case 'F': cout << "Program ending" << endl << endl;
                cin.ignore();
                cout << "\n\nPress Enter to end -->  ";
                cin.ignore();
                return 0;


        default: 
            cout << choice << " is an invalid value. " << endl;

        }

        cout << endl;

    } while( choice != 'f' || choice != 'F');


    return 0;

}

The "issue" I see is that you do not check the bool value of the stream after you read and before you process. To do this, you should put the read as the condition to the while loop.

if( ! inputfile >> integer )
{
    // error code (no integer in file)
    exit(0);
}

LargestValue = integer;
SmallestValue = integer;
NumberOfIntegers++;
SumOfIntegers = SumOfIntegers + integer;

while( inputfile >> integer )
{
    NumberOfIntegers++;
    SumOfIntegers = SumOfIntegers + integer;

    //inputfile >> integer;
    if( integer > LargestValue || integer < SmallestValue)
    {

        if ( integer > LargestValue)
            LargestValue = integer;

        else 
            SmallestValue = integer;
    }
}

In this scenario, the result should be the same with your program because if the inputfile >> integer failed, I believe integer keeps the same value as before so it would not effect LargestValue or SmallestValue . Then you check the stream so NumberOfIntegers and SumOfIntegers won't be updated, which is correct. The only time your program would give undefined results (for LargestValue and SmallestValue ) is if the file doesn't start with an integer and simply by checking the first read and handling it appropriately this will be fixed.

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