i am working on a program for a project where we are given a txt file containing a country, its number of gold, silver, and bronze medals, and its population. we have to read from it and output the country with the most gold, silver,bronze medals, most medals overall, and the country with the highest medals per capita. i have pretty much gotten the formulas down pack. when I run it the number values are correct but the corresponding country is incorrect and i cant quite seem to get the medals per capita formula right.
here is what i have so far

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

using namespace std;

int main()
{
string country[25];
int m, g, s, b, p, mpc[25], totalMedals[25], gold[25], silver[25], bronze[25];
double population[25];

m = totalMedals[0];
g = gold[0];
s = silver[0];
b = bronze[0];
p = mpc[0];

ifstream indata;
indata.open("medals.txt");

ofstream outdata;
outdata.open("winners.txt");

int i=0;

indata >> country[i] >> gold[i] >> silver[i] >> bronze[i] >> population[i];

while(!indata.eof())
{
// most medals calculationsss
totalMedals[i] = gold[i] + silver[i] + bronze[i];

{
if (totalMedals[i] > m)
m = totalMedals[i];
}

mpc[i] = totalMedals[i] / population[i];
{
if ( mpc[i] > p)
p = mpc[i];
}

{
if (gold[i] > g)
g = gold[i];
}

{
if (silver[i] > s)
s = silver[i];
}

{
if (bronze[i] > b)
b = bronze[i];
}

indata >> country[i] >> gold[i] >> silver[i] >> bronze[i] >> population[i];

}
cout << country[i] << " won the most medals at a total of " << m << " medals.\n";
cout << country[i] << " got the most Gold medals at a total of " << g << " medals.\n";
cout << country[i] << " got the most Silver medals at a total of " << s << " medals.\n";
cout << country[i] << " got the most Bronze medals at a total of " << b << " medals.\n";
cout << country[i] << " had the greatest medal per capita value of " << p << ".\n";

indata.close();
outdata.close();

return 0;
}

this is the output when i run it

jap won the most medals at a total of 112 medals.
jap got the most Gold medals at a total of 43 medals.
jap got the most Silver medals at a total of 45 medals.
jap got the most Bronze medals at a total of 36 medals.
jap had the greatest medal per capita value of 0.
Press any key to continue . . .
According to your output, the last country in the text file is jap. Am I?
this is the because, the last i value executed in the while loop was 24. so it will be used throughout in this code
 ``12345678`` ``````cout << country[i] << " won the most medals at a total of " << m << " medals.\n"; cout << country[i] << " got the most Gold medals at a total of " << g << " medals.\n"; cout << country[i] << " got the most Silver medals at a total of " << s << " medals.\n"; cout << country[i] << " got the most Bronze medals at a total of " << b << " medals.\n"; cout << country[i] << " had the greatest medal per capita value of " << p << ".\n"; If you give the exact question and the input txt file, I can help. because your variable names are difficult to understand and follow.``````

Last edited on
medals.txt

usa 43 34 23 300000000
chn 31 45 36 1000000000
jam 9 2 3 5000000
grb 12 13 14 50000000
jap 2 21 11 125000000

your right but how do I fix this? and my medals per capita is completely wrong...it is not printing the right country or number. yeah I know sorry I was in a rush to do this because it is due today and also because I had no clue what I was doing. haha. but if you could please help! I would really appreciate it
The reason for getting zero medal per capita is
for e.g: lets take great Britain
popluation is 50000000
total medals is 29
medal per capita = 29/50000000(int/double)
= 0.00000078
= 0(because according to ur code mpc is an integer

So after altering here and there, your code will be
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` `````` #include #include #include #include using namespace std; int main() { ifstream indata; indata.open("medal.txt"); ofstream outdata; outdata.open("winners.txt"); string country[5]; double medal_per_capita[5]; int gold[5], silver[5], bronze[5]; double totalMedals[5]={0}; int population[5]; int max_gold=0; int max_silver=0; int max_bronze=0; int max_medal=0; int max_capita=0; //cout<> country[i] >> gold[i] >> silver[i] >> bronze[i] >> population[i]; totalMedals[i]+=gold[i]+silver[i]+bronze[i]; medal_per_capita[i]=totalMedals[i]/population[i]; } for(int i=1;i<5;i++) { if(totalMedals[i]>totalMedals[max_medal]) { max_medal=i; } if(gold[i]>gold[max_gold]) { max_gold=i; } if(silver[i]>silver[max_silver]) { max_silver=i; } if(bronze[i]>bronze[max_bronze]) { max_bronze=i; } if(medal_per_capita[i]

Check with maximum capita formula. I dont know how to show max_per_Capita value with decimal places instead of enum values. Because im also a beginner.
:)

Thank you so much!!! You are awesome on so many levels!
You are welcome & thank you
Last edited on
Topic archived. No new replies allowed.