C ++ while循环读取输入

时间:2014-03-18 19:13:23

标签: c++ file-io

我提交了这个程序并且效果很好,但是我的老师说我的while循环有问题,即使我得到了正确的答案。任何提示或帮助?

当到达文件末尾并且第43行的while变为无效时,read循环会发生什么?你的程序结构的方式你没有看到问题,但它就在那里。应重新调整while循环以解决此问题。

#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;

}

1 个答案:

答案 0 :(得分:3)

我看到的“问题”是您在阅读之后和处理之前不检查流的bool值。为此,您应该将read作为条件放入while循环。

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;
    }
}

在这种情况下,结果应该与您的程序相同,因为如果inputfile >> integer失败,我相信integer保持与之前相同的值,因此它不会影响LargestValueSmallestValue。然后检查流,以便NumberOfIntegersSumOfIntegers不会更新,这是正确的。您的程序唯一一次给出未定义的结果(对于LargestValueSmallestValue),如果文件不是以整数开头,只需检查第一次读取并正确处理它就会得到修复。