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
}
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.
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
#include <iostream>
#include <string>
#include <fstream>
#include<iomanip>
usingnamespace 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.
:)