Question about reading files/finding max number and name

So I am working on a project currently, that takes a name, gets 5 judge scores, finds the highest and lowest, and finds the average minus the highest and lowest.

For each contestant, I am print their name and average score to a text file. The file created looks like this:

John 8.3
Matt 7.1
Josh 4.5

I am getting everything in the program just fine, but cannot figure out how to create a function that will read the file, find the highest average and return the average and name. I can write a function that returns the highest number, but how can I make it return both the name and the number?

Hope this makes sense...

Read in names to an array, and numbers to an array.

When you find the highest number in array location X, you know that the corresponding name is in name location X.
Will this work even if there is an indefinite number of names and scores? Don't arrays have to be pre-defined in the number of variables they accept? I was kind of wondering if I could create a conditional that reads through the file, sets a max value, compares each score to the max value and then returns a cout statement

cout << "the highest score is " << name << " with a score of " << score << endl;

is that not possible?
Last edited on
This being C++, don't use an array anyway. Use a vector.

What you want is completely possible, although where you say "conditional" I wonder if you mean "function".
That is what I meant, a function...I am just not sure how to return a cout statement like that from a function.

I guess I meant I would have a conditional in my function that reads scores through to the end, set the "max" variable to zero, compares each score to the one before it to see if it is larger, make the largest the max, and then somehow return the max and connected name.

I am still a bit confused, and would post code, but sadly, I am at work.
Here is my code for that function so far:

(How do I make this look like code on this forum? Is there a tag?)



void findWinner(string name, double score, string winner, double winningScore){

ifstream in;
in.open("scores.txt");
winningScore = 0;
score = 0;
while (in >> name >> score){

if (score > winningScore){

winner = name;
winningScore = score;
}

cout << winningScore << endl;

in.close();
}
}



So each time I do this, whether I enter 3, 5, or 2 people into my file, it always returns the first score no matter what. I know I am missing something, but just cannot figure it out. I just need it to go through the file and return the highest score and name.
The problem is

while (in >> name >> score){

What I believe you want is

1
2
while (in){
	in >> name >> score;{
Last edited on

Excellent! Thank you. I am also currently trying to figure out a way to make it so that if two people get the same score, the first score read is always the winner. I am thinking that I can use an if statement, but am not sure how to implement it.
I can get it to print the tie message, but never the first score based on the else statement.



void findWinner(string name, double score, string winner, double winningScore){

ifstream in;
in.open("scores.txt");
winningScore = 0;
score = 0;
while (in){

in >> name >> score;

if (score > winningScore){
winner = name;
winningScore = score;
}else{
cout <<"There was a tie, winner is first judged." << endl;
break;
}



cout << winner << " "<< winningScore << endl;

in.close();
}
Last edited on
Notice that in the function heading:
void findWinner(string name, double score, string winner, double winningScore){

you assign winningScore.

But then in the function body:

winningScore = 0;

So, no matter what your argument for winningScore is, winningScore == 0
so,

if (score > winningScore){
will be true as long as score > 0.
I tried removing the zero, and still cannot get my if statement to return the first instance of a certain score in case of a tie. Not sure what I'm doing wrong.
Hmm, sorry I'm not sure.

Your if statements looks to me like it should work.

It's easier to read the code if you clean it up a bit

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
void findWinner(string name, double score, string winner, double winningScore /*Do you need all these paremeters?*/)
{
ifstream in;
in.open("scores.txt");
 
winningScore = 0;
score = 0;
 
while (in)
{	
 	in >> name >> score;

 	if (score > winningScore)
	{
 		winner = name;
 		winningScore = score;
 	}
	else if (score == winningScore)
	{
 		cout <<"There was a tie, winner is first judged." << endl;
 	}

	cout << winner << " "<< winningScore << endl;

in.close();
} 


I added

else if (score == winningScore)

So that in the case that score < winningScore, the print "There was a tie" is skipped.

You have some problems with parameters in the function heading that don't seem to be used in the body. But the if statements looks ok.

Hope that helps. Good luck.
I figured it out! Thank you all for your help.
Topic archived. No new replies allowed.