| | |
overlapping array of stucts function
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
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;
};
any help would be appreciated
entire code is in attachment
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)
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
Last edited by kyosuke0; Sep 14th, 2008 at 10:37 pm.
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?
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
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?
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.
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
And
is a potential endless loop. eof() will never become true if something goes wrong during a read operation.
C++ Syntax (Toggle Plain Text)
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
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
vmanes isn't suggesting that you take out thebreakstatement that have. He's suggesting that you add ANOTHERbreakstatement.
ill test and see if what was suggested works.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Assignment Help
- Next Thread: Binary Multiplication
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






