help pleaseee!

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
1
2
3
4
5
6
7
8
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include <iostream>
#include <string>
#include <fstream>
#include<iomanip>
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<<setprecision(8);
	for(int i=0;i<5;i++)
	{
		indata >> 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]<totalMedals[max_capita])
		{
			max_capita=i;
		}
	}
cout << country[max_medal]<<left<<setw(45) << " won the most medals at a total of " << totalMedals[max_medal] << " medals.\n";
cout << country[max_gold]<<left<<setw(45) << " got the most Gold medals at a total of " << gold[max_gold] << " medals.\n";
cout << country[max_silver]<<left<<setw(45) << " got the most Silver medals at a total of " << silver[max_silver] << " medals.\n";
cout << country[max_bronze] <<left<<setw(45)<< " got the most Bronze medals at a total of " << bronze[max_bronze] << " medals.\n";
cout << country[max_capita] <<left<<setw(45)<< " had the greatest medal per capita value of "<<medal_per_capita[max_capita] << ".\n";



	

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

	system("pause");
	return 0;
}


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.