2017-03-09 6 views
-1

私はカンマ区切りのデータを.txtファイルから読み込み、2つの異なる配列(名前は& indScores)に解析しようとしています。 indScores []から、特定の名前の全体平均を取得し、それをavg_scores []に格納しています。最後に、読み込まれた行全体を返します。ファイルから入力を読み取るときに配列インデックスが正しくない

サンプルの入力データは、次のようになります。

name1,x1,x2,x3,x4,x5 
    name2,y1,y2,y3,y4,y5 
    name3,z1,z2,z3,z4,z5 
    name4,a1,a2,a3,a4,a5 
    .... 

マイアレイの出力この

names[name2, name4, name6, name8,...] 
    avg_scores[x_avg, x_avg + y_avg, x_avg + y_avg + z_avg,...] 

そして、私の全体の行数は、私が期待していものの半分です。間違った位置でインデックスを作成しているのですか、それとも自分のロジックが間違っていますか?

int ReadScores(string fileName, string names[], float avg_scores[], int array_size){ 

float indScores[array_size]; 

int lineCounter = 0; 
string myLine, nameSubString, scoreSubString; 
float scoreConvert = 0.0; 
float averageScores = 0.0; 

ifstream myFileIn; 
//open the file 
myFileIn.open(fileName, ios::in); 
    if (myFileIn.fail()){ 
     cout << "Error opening "<< fileName << endl; 
     return 0; 
    } 
    int index = 0; 
    //read the file with a while loop until the end of file is reached 
    while (getline(myFileIn, myLine)){ 
      averageScores; 
      getline(myFileIn, myLine); 
      //firstComma will hold the integer value of the index position of the first comma found 
      int firstComma = myLine.find(','); 
      //this should grab the the names at the beginning of each string on each new line 
      nameSubString = myLine.substr(0, firstComma); 
      names[index] = nameSubString; 


      int startingPos = 0; 
      float commaCounter = 0; 
      //find how many commas are in a string and use that to limit your loop 
      for (int ind = 0; ind < myLine.length(); ind++){ 
       if (myLine[ind] == ',') 
        commaCounter++; 
       } 

      for (int ind = 0; ind < commaCounter; ind++){ 
        //grab the first number and store it the scoreSubString variable 
        //this tells the myLine.substr to start after the very first comma 
        int found = myLine.find(',', firstComma) + 1; 
        scoreSubString = myLine.substr(found, myLine.find(',')); 
        //change the value of firstComma to the next index location 
        firstComma = found + 1; 

        ///convert string to number 
        stringstream(scoreSubString) >> scoreConvert; 
        ///store number in float array 
        indScores[ind] = scoreConvert; 
       } 

      for (int ind = 0; ind < commaCounter; ind++){ 
       averageScores = indScores[ind] + averageScores; 
       } 
       float averageOverall = averageScores/commaCounter; 
       //store the averageOverall into the avg_scores [] 
       avg_scores[index] = averageOverall; 

     index++; 
     lineCounter++; 
     } 
    myFileIn.close(); 
return lineCounter; 
} 
+0

'getline(myFileIn、myLine)'をループの各繰り返しで2回呼び出します.1回は 'while'内に、もう一度2行は下に移動します。あなたは効果的に他のすべての行をスキップしています。 –

答えて

0

いいえ、一度それを削除すると、getline(myFilenIn, myLine)のものが一致し始めました。

私も自分のaverageScoresを取得し、それは私が常に初めから開始したことなくて、私の.find(',', found)の開始位置foundを追加しましたforループの前に私のaverageScores = 0をリセットする必要がありました。

私の新しいコードは次のようになります。

int ReadScores(string fileName, string names[], float avg_scores[], int array_size){ 

int linesCounted = 0; 
float indScores[array_size]; 

int lineCounter = 0; 
string myLine, nameSubString, scoreSubString; 
float scoreConvert = 0.0; 
float averageScores = 0.0; 

ifstream myFileIn; 
//open the file 
myFileIn.open(fileName, ios::in); 
    if (myFileIn.fail()){ 
     cout << "Error opening "<< fileName << endl; 
     return 0; 
    } 
    int index = 0; 
    //read the file until the end of file is reached 
    while (getline(myFileIn, myLine)){ 
      //firstComma will hold the integer value of the index position of the first comma found 
      int firstComma = myLine.find(','); 
      //this should grab the the names at the beginning of each string on each new line 
      nameSubString = myLine.substr(0, firstComma); 
      names[index] = nameSubString; 


      int startingPos = 0; 
      float commaCounter = 0; 
      //find how many commas are in a string and use that to limit your loop 
      for (int ind = 0; ind < myLine.length(); ind++){ 
       if (myLine[ind] == ',') 
        commaCounter++; 
       } 

      for (int ind = 0; ind < commaCounter; ind++){ 
        //grab the first number and store it the scoreSubString variable 
        int found = myLine.find(',', firstComma) + 1; 
        scoreSubString = myLine.substr(found, myLine.find(',', found)); 
        //change the value of firstComma to the next index location 
        firstComma = found + 1; 

        ///convert string to number 
        stringstream(scoreSubString) >> scoreConvert; 
        ///store number in float array 
        indScores[ind] = scoreConvert; 
       } 

     averageScores = 0; 
      for (int ind = 0; ind < commaCounter; ind++){ 
       averageScores = indScores[ind] + averageScores; 
       } 
       float averageOverall = averageScores/commaCounter; 
       avg_scores[index] = averageOverall; 
     index++; 
     if (!myLine.empty()){ 
       lineCounter++; 
      } 
     } 
    myFileIn.close(); 

リターンlineCounter。 }

関連する問題