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?
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;
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.
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.
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.
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.