943,767 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1365
  • C++ RSS
Sep 14th, 2008
0

overlapping array of stucts function

Expand Post »
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;
};

C++ Syntax (Toggle Plain Text)
  1. void arryRead(Data ary[SIZE])
  2. {
  3. int year;
  4. string event, firstName, lastName, medal, country, inputFile;
  5. double time;
  6. bool flag = true;
  7. ifstream inFile;
  8.  
  9. while (flag == true)
  10. {
  11. cout << endl << "Enter file name: ";
  12. cin >> inputFile;
  13. cout << endl;
  14.  
  15. inFile.open(inputFile.c_str());
  16. if (!inFile)
  17. {
  18. cout << "Cannot open the input file. Try agin." << endl;
  19. }
  20. else
  21. {
  22. flag = false;
  23. }
  24. }
  25.  
  26.  
  27.  
  28. while (!inFile.eof())
  29. {
  30. inFile >> year >> event >> firstName >> lastName >> medal
  31. >> country >> time;
  32. for(int g = 0; g < SIZE; g++)
  33. {
  34. if (ary[g].nation == country)
  35. {
  36.  
  37. if (medal == "GOLD")
  38. ary[g].award1++;
  39. else if (medal == "SILVER")
  40. ary[g].award2++;
  41. else if (medal == "BRONZE")
  42. ary[g].award3++;
  43. }//end if statment
  44. if (ary[g].nation == "xxx")
  45. {
  46. ary[g].nation = country;
  47. if (medal == "GOLD")
  48. ary[g].award1++;
  49. else if (medal == "SILVER")
  50. ary[g].award2++;
  51. else if (medal == "BRONZE")
  52. ary[g].award3++;
  53. break;
  54. }
  55. }
  56.  
  57. }//end while loop
  58. inFile.close();
  59. cout << ary[0].nation << endl << ary[1].nation << ary[2].nation << ary[3].nation
  60. << ary[4].nation << ary[5].nation << ary[6].nation << ary[7].nation;
  61. }//end function

any help would be appreciated
entire code is in attachment
Attached Files
File Type: txt proj.data.txt (8.9 KB, 15 views)
Last edited by kyosuke0; Sep 14th, 2008 at 10:37 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Sep 14th, 2008
0

Re: overlapping array of stucts function

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?
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 15th, 2008
0

Re: overlapping array of stucts function

Click to Expand / Collapse  Quote originally posted by vmanes ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Sep 15th, 2008
0

Re: overlapping array of stucts function

Click to Expand / Collapse  Quote originally posted by kyosuke0 ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Sep 15th, 2008
0

Re: overlapping array of stucts function

And

C++ Syntax (Toggle Plain Text)
  1. while (!inFile.eof())

is a potential endless loop. eof() will never become true if something goes wrong during a read operation.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Sep 15th, 2008
0

Re: overlapping array of stucts function

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Sep 15th, 2008
0

Re: overlapping array of stucts function

Click to Expand / Collapse  Quote originally posted by vmanes ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Assignment Help
Next Thread in C++ Forum Timeline: Binary Multiplication





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC