Hello there, My program is supposed to read data from a txt file, and put this data in a 2 dimension array. All I have is the last line of data in my text file. My teacher said that I have to update the array in my loop. I have no idea how to do it. Can someone help me? Nicole
example of txt file

1 2 3
4 5 6
...

int main()
 { 
    const int TYPE = 3; 
    const int COUNTRY = 50;

   int result[COUNTRY][TYPE] = {0};
   int totalPoint[COUNTRY] = {0};
   int numGold,numSilver,numBronze;
   int cntCountry = 0;
   int totalMedal = 0;


   while(!cin.eof() )
   {

       cin >> numGold >> numSilver >> numBronze;

       if(numGold > 0 || numSilver > 0 || numBronze > 0)
       {
            ++cntCountry;
       }
   }


   for ( int i = 0; i < cntCountry; ++i )
             {
            result[i][0] = numGold;
            result[i][1] = numSilver;
            result[i][2] = numBronze;


      }

Edited 3 Years Ago by Dani: Formatting fixed

> while(!cin.eof() )
This is a bad idea and will probably result in you processing the last line of the file twice. A better method is to use your input as the condition:

while ( cin >> numGold >> numSilver >> numBronze )

Without knowing the format of your file, I have to assume that your teacher meant this change:

result[i][0] += numGold;
result[i][1] += numSilver;
result[i][2] += numBronze;

At the end of the file, the array has a total of everything rather than the values of the last line.

This article has been dead for over six months. Start a new discussion instead.