In: Computer Science
C++ Programming
Consider an input file that lists test participants in alphabetical order and their scores. Each line has the following format:
LastName FirstName score
Load the input file into an array of structs, knowing there is a maximum of 50 participants. Output to output.txt the participants with the top 5 scores, in decreasing order of scores. Each output line should have the following format:
FirstName,LastName,score
Notes:
Example input file:
Baker Alex 90 Eaves Pat 100 Kay Ivy 100 Lamar Jay 80, O'Conor Ashton 95 Saber Alice 98 Sabin Chris 89 Valdez Daniel 99 Valko Pete 92
Example output file:
Pat,Eaves,100 Ivy,Kay,100 Daniel,Valdez,99 Alice,Saber,98 Ashton,O'Conor,95
#include <iostream>
using namespace std;
struct participant
{
string fname;
string lname;
int score;
};
int main() {
/* Type your code here. */
return 0;
}
If you have any doubts, please give me comment...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct participant
{
string fname;
string lname;
int score;
};
int main()
{
string inp_fname;
cout << "Enter input filename: ";
cin >> inp_fname;
ifstream in;
in.open(inp_fname.c_str());
ofstream out;
out.open("output.txt");
if (in.fail())
{
out << "File not found!" << endl;
return -1;
}
participant participants[50];
string fname, lname;
int score, n = 0;
while (!in.eof())
{
in >> lname >> fname >> score;
participants[n].lname = lname;
participants[n].fname = fname;
participants[n].score = score;
n++;
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (participants[i].score < participants[j].score || (participants[i].score == participants[j].score && participants[i].fname < participants[j].fname))
{
participant temp = participants[i];
participants[i] = participants[j];
participants[j] = temp;
}
}
}
for(int i=0; i<5; i++){
out<<participants[i].fname<<","<<participants[i].lname<<","<<participants[i].score<<endl;
}
in.close();
out.close();
return 0;
}