Consider a text file named scores.txt that contains player scores for a game. A
ID: 3679398 • Letter: C
Question
Consider a text file named scores.txt that contains player scores for a game. A possible sample is shown here where Ronaldo's best score is 10400, Didier's best score is 9800, etc. Ronaldo 10400 Didier 9800 Pele 12300 Kaka 8400 Cristiano 8000 Write a function named getHighScores that takes a string reference parameter and an integer reference parameter. The function should scan through the file and set the reference parameters to the name of the player with the highest score and the corresponding score. Output: $ ./program04b The highest score is 12300 by Pele
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int getHighScore(string name, int value);
static int store_value=0;
string store_name; //store_value
int main()
{
int value;
string name;
ifstream stream;
stream.open("Score.txt"); //Score
while(stream>>name>>value){
cout << name << " " << value ;
getHighScore(name, value); //getHighScore
cout << endl;
}
cout << endl << endl << "Result : " << store_name << ", " << store_value << endl;
stream.close(); //Score
system("pause");
return 0;
}
int getHighScore(string name, int value)
{
if(value > store_value){
store_value=value;
store_name=name;
cout << " Curret Best Score : " << store_name << " , " << store_value;
}
return 0;
}
Score.txt
Ronaldo
10400
Didier
9800
Pele
12300
Kaka
8400
Cristiano
8000
sample output
Ronaldo 10400 Curret Best Score : Ronaldo , 10400
Didier 9800
Pele 12300 Curret Best Score : Pele , 12300
Kaka 8400
Cristiano 8000
Result : Pele, 12300
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.