2014-03-18 50 views
0

我提交了這個程序,它的工作原理完美,但我的老師說我的while循環出了問題,即使我得到了正確的答案。任何提示或幫助?循環讀取輸入的C++

當達到文件結尾且第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; 

} 
+0

在使用該值之前,您從不檢查輸入是否成功。 – chris

+0

你有一位好老師。 – 0x499602D2

回答

3

的「問題」我看到的是你,你看,你以前處理後不檢查流的bool值。要做到這一點,你應該把閱讀作爲條件的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),如果文件不是以整數開始,並且只是通過檢查第一次讀取並適當處理它,這將被修復。

+0

我明白,如果從文件中讀取整數,while循環的條件應該關閉。非常感謝 –