overlapping array of stucts function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

overlapping array of stucts function

 
0
  #1
Sep 14th, 2008
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;
};

  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
Last edited by kyosuke0; Sep 14th, 2008 at 10:37 pm.
Attached Files
File Type: txt proj.data.txt (8.9 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: overlapping array of stucts function

 
0
  #2
Sep 14th, 2008
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?
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: overlapping array of stucts function

 
0
  #3
Sep 15th, 2008
Originally Posted by vmanes View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: overlapping array of stucts function

 
0
  #4
Sep 15th, 2008
Originally Posted by kyosuke0 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: overlapping array of stucts function

 
0
  #5
Sep 15th, 2008
And

  1. while (!inFile.eof())

is a potential endless loop. eof() will never become true if something goes wrong during a read operation.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: overlapping array of stucts function

 
0
  #6
Sep 15th, 2008
Originally Posted by VernonDozier View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: overlapping array of stucts function

 
0
  #7
Sep 15th, 2008
Originally Posted by vmanes View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC