簡體   English   中英

C ++循環讀取輸入

[英]C++ while loop reading input

我提交了該程序,並且運行良好,但是我的老師說,即使我得到正確的答案,我的while循環還是有問題。 有任何提示或幫助嗎?

當到達文件末尾並且第43行的read無效時,在while循環中會發生什么? 程序的結構方式雖然看不到問題,但確實存在。 應該重組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;

}

我看到的“問題”是您在閱讀之后和處理之前不檢查流的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 )的時間是,如果文件不是以整數開頭的,只需檢查第一次讀取並適當地處理它,即可解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM