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

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

using namespace std;

const int SIZE = 30;

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

void initialize(Data ary[]);
void arryRead(Data ary[]);
void menu(Data ary[]);
void firstOption(const Data ary[]);
void secondOption(const Data ary[]);
void thirdOption(const Data ary[]);
void fourthOption(const Data ary[]);

int main()
{
    
    Data ary[SIZE];
 
    
    initialize(ary);
 
    arryRead(ary);
 
    menu(ary);
 
    return 0;
}

//function to initialize array
void initialize(Data ary[SIZE])
{
     for(int i = 0; i < SIZE; i++)
     {
             ary[i].nation = "xxx";
             ary[i].award1 = 0;
             ary[i].award2 = 0;
             ary[i].award3 = 0;
     }
     
}

//function that reads data into array
void arryRead(Data ary[SIZE])
{
     int year, y = 0;
     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

//menu function
void menu(Data ary[SIZE])
{
     int option;
     
     do
     {
         cout << endl << "Option 1: Displays all data for a country."
              << endl << endl;
         cout << "Option 2: Displays the total number of medals for that medal."
              << endl << endl;
         cout << "Option 3: Displays the country that has won the most of that"
              << " medal." << endl << endl;
         cout << "Option 4: Displays the total number of each type of medal "
              << "won." << endl << endl;
         cout << "Option 5: Exit" << endl << endl;
         cout << "Enter number of option: ";
         cin >> option;
         cout << endl;
         
         switch (option)
         {
                case 1:
                     firstOption(ary);
                     break;
                case 2:
                     secondOption(ary);
                     break;
                case 3:
                     thirdOption(ary);
                     break;
                case 4:
                     fourthOption(ary);
                     break;
                case 5:
                     break;
                default:
                     cout << endl << "Invalid choice." << endl;
                     break;
          }// end switch 
      }// end do
      while (option != 5);
}// end function

//1st option function
void firstOption(const Data ary[SIZE])
{
     string countryCode;
     bool flag = false;
     
     cout << endl << "Enter name of country.";
     cin >> countryCode;
     
     while (flag == false)
     {
           for(int j = 0; j < SIZE; j++)
           {
                   if (countryCode == ary[j].nation)
                   {
                      cout << endl;
                      cout << "Country: " << countryCode << endl;
                      cout << "Gold: " << ary[j].award1 << endl;
                      cout << "Silver: " << ary[j].award2 << endl;
                      cout << "Bronze: " << ary[j].award3 << endl;
                      flag = true;
                   }//end if statment
           }//end for loop
           
           if (flag == false)
           {
              cout << endl << "Invalid country code. Try agin." << endl;
              cin >> countryCode;
           }//end if statment
     }//end while loop
}//end function

//2nd option function
void secondOption(const Data ary[SIZE])
{
     string medalType;
     int num = 0;
     bool flag = true;
     
     cout << endl << "Enter type of medal.";
     cin >> medalType;
     
     while (flag == true)
     {
           if (medalType == "GOLD")
           {
              for(int x = 0; x < SIZE; x++)
              {
                      num = num + ary[x].award1;
              }
              flag = false;
              cout << endl << "Number of Gold medals: " << num << endl;
           }
           else if (medalType == "SILVER")
           {
                for(int f = 0; f < SIZE; f++)
                {
                        num = num + ary[f].award2;
                }
                flag = false;
                cout << endl << "Number of silver medals: " << num << endl;
           }
           else if (medalType == "BRONZE")
           {
                for(int k = 0; k < SIZE; k++)
                {
                        num = num + ary[k].award3;
                }
                flag = false;
                cout << endl << "Number of Silver medals: " << num << endl;
           }
           else
           {
               cout << endl << "Invalid medal choice. Try agin.";
               cin >> medalType;
           }
     }//end while
}//end function

//3rd function
void thirdOption(const Data ary[SIZE])
{
     string medalType, country;
     bool flag = true;
     int num = 0;
     
     cout << endl << "Enter a medal type.";
     cin >> medalType;
     
     while (flag == true)
     {
           if (medalType == "GOLD")
           {
              for(int m = 0; m < SIZE; m++)
              {
                      if(num < ary[m].award1)
                      {
                             num = ary[m].award1;
                             country = ary[m].nation;
                      }
              }
              flag = false;
              cout << endl << "Country: " << country << endl << "Gold medals: "
                   << num << endl;
           }
           else if (medalType == "SILVER")
           {
                for(int z = 0; z < SIZE; z++)
                {
                        if (num < ary[z].award1)
                        {
                                num = ary[z].award2;
                                country = ary[z].nation;
                        }
                }
                flag = false;
                cout << endl << "Country: " << country << endl
                     << "Silver medals: " << num << endl;
           }
           else if (medalType == "BRONZE")
           {
                for(int y = 0; y < SIZE; y++)
                {
                        if (num < ary[y].award3)
                        {
                                num = ary[y].award3;
                                country = ary[y].nation;
                        }
                }
                flag = false;
                cout << endl << "Country: " << country << endl
                     << "Bronze medals: " << num << endl;
           }
           if (flag == true)
           {
                    cout << endl << "Invalid choice. Try agin.";
                    cin >> medalType;
           }
     }// end while loop
}//end function

//4th option
void fourthOption(const Data ary[SIZE])
{
     int gCount = 0; 
     int sCount = 0; 
     int bCount = 0;
     
     for(int n = 0; n < SIZE; n++)
     {
             gCount = gCount + ary[n].award1;
             sCount = sCount + ary[n].award2;
             bCount = bCount + ary[n].award3;
     }
     cout << endl << "Gold: " << gCount;
     cout << endl << "Silver: " << sCount;
     cout << endl << "Bronze: " << bCount << endl;
}

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.

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.

This question has already been answered. Start a new discussion instead.