2016-12-07 6 views
1

を持つファイルからwhileループ私は、この情報を含むファイルがあります:C++異なるデータ型

Bev Powers 
3 
76 81 73 
Chris Buroughs 
5 
88 90 79 81 84 
Brent Mylus 
2 
79 81 

を、私は最初の3行を行うと、正しく情報を使用する、カウント制御ループを持っているが、私は苦労しています一致したゴルファーの数に関係なく、すべての情報がファイルから表示されるまで、ループを再利用するループがあります。正しい方向に指針を求めていますが、何か助けていただければ幸いです。

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

ifstream inScores; 
string filename; 

string name; 

int loopCount, matchScore; 

int count = 1; 
float mean = 0; 
float adder = 0; 

int main() 
{ 
    cout << endl << "Enter the golfer's filename: "; 
    getline(cin,filename); 
    cout << endl; 

    inScores.open(filename.c_str()); 
     if(!inScores) 
     { 
      cout << "** " << filename << " does not exist. Please "; 
      cout << "check the spelling and rerun "; 
      cout << "the program with an existing golfer file. ** " << endl << endl; 

      return 1; 
     } 


    getline(inScores,name); 
    inScores >> loopCount; 

    cout << name << " has " << loopCount << " matches with scores of" << endl << endl; 

    inScores >> matchScore; 
    while (count <= loopCount) 
    { 
      cout << "Match " << count << ": " << matchScore << endl; 
      adder = adder + matchScore; 
      adder = adder + matchScore; 
      inScores >> matchScore; 
      count++; 
    } 

    cout << endl; 
    int(mean) = .5 + (adder/loopCount); 
    cout << "The mean score is " << mean << endl << endl; 

inScores.close(); 
return 0; 
} 
+3

最初のステップは、 '>>'を取り除くことです。 'std :: getline'と' >> 'を混在させると[予期しない結果が生じる](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction)。 'std :: getline'を使って3行のテキストを読みます。 2行目と3行目の 'std :: istringstream'を作成し、' std :: istringstream'を使って解析します。 –

+1

最初の2行を読み、次のN値のforループを使用して、すべてをwhileループにラップします。おそらく関数のN値の読みを置くのが最も良いでしょう、それはもっときれいでしょう。 –

答えて

0

あなたが望むものを得るためには、ループを使用する必要があります。また、getlineと抽出が失敗した場合は、ループ内のテストに使用できます。

ifstream inScores; 
string filename; 

string name; 

int loopCount , matchScore; 

int count = 1; 
float mean = 0; 
float adder = 0; 

int main() 
{ 
    cout << endl << "Enter the golfer's filename: "; 
    getline(cin , filename); 
    cout << '\n'; 

    inScores.open(filename); 
    if (!inScores) 
    { 
     cout << "** " << filename << " does not exist. Please "; 
     cout << "check the spelling and rerun "; 
     cout << "the program with an existing golfer file. ** " << "\n\n"; 

     return 1; 
    } 
    while (getline(inScores , name)) 
    { 
     if (inScores >> loopCount) 
     { 
      cout << name << " has " << loopCount << " matches with scores of" << "\n\n"; 
     } 
     else 
     { 
      cout << "File read error"; 
      return 1; 
     } 

     for (int count = 1; count <= loopCount; count++) 
     { 
      if (inScores >> matchScore) 
      { 
       cout << "Match " << count << ": " << matchScore << '\n'; 
       adder = adder + matchScore; 

      } 
      else 
      { 
       cout << "File read error"; 
       return 1; 
      } 
     } 

    } 
    cout << '\n'; 
    int(mean) = .5 + (adder/loopCount); 
    cout << "The mean score is " << mean << "\n\n"; 

    inScores.close(); 
    return 0; 
} 
関連する問題