My goal is to read Olympic data into an array of structs. What i need to read in is the country code, and number of gold, silver or bronze medals that the country won. Also if one country won more than one medal, i am to just increment the information already read in on that country. My problem is that i cant get the function to overlap and just increment instead of just reading it into the next array index.

sample input file:
1987 100_Men Tom Burke GOLD USA 12.0
1875 100_Men Frits Hofman SILVER DEU 12.2
1958 100_Men Francis Lane BRONZE USA 12.6
1928 100_Men Elizabeth Robinson GOLD USA 12.2
......

my function code
SIZE is a const of 30 which is the max of countries and i initialized all the array members of nation to "xxx"

struct Data
{
string nation;
int award1;
int award2;
int award3;
};

void arryRead(Data ary[SIZE])
{
     int year;
     string event, firstName, lastName, medal, country, inputFile;
     double time;
     bool flag = true;
     ifstream inFile;
     
     while (flag == true)
     {
           cout << endl << "Enter file name: ";
           cin >> inputFile;
           cout << endl;
     
           inFile.open(inputFile.c_str());
           if (!inFile)
           {
               cout << "Cannot open the input file. Try agin." << endl;
           }
           else
           {
               flag = false;
           }
      }
      
      
                  
     while (!inFile.eof())
     {
            inFile >> year >> event >> firstName >> lastName >> medal
                  >> country >> time;
            for(int g = 0; g < SIZE; g++)
            {
                    if (ary[g].nation == country)
                    {
                      
                       if (medal == "GOLD")
                          ary[g].award1++;
                       else if (medal == "SILVER")
                            ary[g].award2++;
                       else if (medal == "BRONZE")
                            ary[g].award3++;
                    }//end if statment
                    if (ary[g].nation == "xxx")
                    {
                         ary[g].nation = country;
                         if (medal == "GOLD")
                            ary[g].award1++;
                         else if (medal == "SILVER")
                              ary[g].award2++;
                         else if (medal == "BRONZE")
                              ary[g].award3++;
                         break;
                    }
            }
            
     }//end while loop
     inFile.close();
     cout << ary[0].nation << endl << ary[1].nation << ary[2].nation << ary[3].nation
          << ary[4].nation << ary[5].nation << ary[6].nation << ary[7].nation;
}//end function

any help would be appreciated
entire code is in attachment

Recommended Answers

All 6 Replies

What about a break statement in the if == country block, otherwise, the for loop continues and adds the entry to the next available slot as well.

Or the problem could be that you've got a woman winning a men's event? ;)

What about a break statement in the if == country block, otherwise, the for loop continues and adds the entry to the next available slot as well.

Or the problem could be that you've got a woman winning a men's event? ;)

the break statement is intended to exit the for loop after it loads the country variable into the member nation with "xxx", if i did not have it in there the for loop would load the first country it gets from the infile into every nation member in the array. This would mean that during its first loop it would load USA into every nation member and i need it to allot USA into just one member and whenever it encounters another USA to just add to the existing one instead of putting it into a new member.

this is my theory anyway, ill test it to make sure when im not so tired.

thanks for the reply, its good to have a fresh mind look at things.

if i did not have it in there the for loop would load the first country it gets from the infile into every nation member in the array.

vmanes isn't suggesting that you take out the break statement that have. He's suggesting that you add ANOTHER break statement.

Member Avatar for jencas

And

while (!inFile.eof())

is a potential endless loop. eof() will never become true if something goes wrong during a read operation.

vmanes isn't suggesting that you take out the break statement that have. He's suggesting that you add ANOTHER break statement.

your right, my mistake. After a couple of hours of sleep i see i read his post completely wrong.

ill test and see if what was suggested works.

What about a break statement in the if == country block, otherwise, the for loop continues and adds the entry to the next available slot as well.

Or the problem could be that you've got a woman winning a men's event? ;)

That did it. Thanks for the help everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.