Hi, I'm having trouble with the following code and any help is greatly appreciated.

``````/*##########################################################################
### The problem should prompt the operator to enter the
number of competitors, followed by the scores awarded to each
by the 5 judges. Scores can range from 0.0 to 6.0 in increments of 0.1.
Invalid scores entered by the operator should result in an error message
being displayed. To calculate the final score to be awarded to each
Diving Competitor, the highest and lowest scores are ignored and
the remaining scores are averaged to give the final score,
which is then displayed. The problem should continue
until specifically terminated.
##########################################################################*/

#include <iostream>
using namespace std;

int main (){  //start of main function

// variable declaration
int numberOfCompetitors; // number of competitors
float judgesScore; // scores awarded by each of the five judges
float finalScore; //final score awarded to competitor
int competitorCounter; // counts number of competitors
int judgeCounter; //counts number of judges
float lowestScore; // lowest score given to competitor
float highestScore; // highest score given to competitor

// initialisation phase
finalScore = 0; //initialise finalScore
competitorCounter = 1; //initialise loop counter
lowestScore = 6.0; // initialise lowest score to 6.0
highestScore = 0.0; // initialise highest score to 0.0
judgeCounter = 1; // initialise number of judges

//display welsome message
cout << "\n Welcome to the Diving Score program which \n determines the final score of each diver\n";
cout << " =========================================\n\n";

cout << "Please enter the number of competitors: "; // prompt user to enter number of competitors
cin >> numberOfCompetitors; // store number of competitors

// Processing phase
while (competitorCounter <= numberOfCompetitors) { //loop for each competitor

//prompt user to enter the scores for the 5 judges
while (judgeCounter <=5){ //loop 5 times
cout << "\nEnter the score for judge " << judgeCounter << ": "; //prompt user to enter judge's score
cin >> judgesScore; //store judges score

if (judgesScore >= 0.0 && judgesScore <= 6.0){
finalScore += judgesScore;  //add judges score to final score

if (judgesScore < lowestScore){ //checks for lowest judge's score
lowestScore = judgesScore;}

if (judgesScore > highestScore){ //checks for highest judge's score
highestScore = judgesScore;}

judgeCounter += 1; //increment judgeCounter by 1
}

else {cout << "You have entered an incorrect value.\nPlease enter a value between 0.0 and 6.0...\n\n";

}            //end if else
}//end while

cout << "highestScore = " << highestScore;
cout << "\nloweastScore = " << lowestScore <<endl;

finalScore = (finalScore - highestScore - lowestScore) / 3; //calculate final score for competitor
cout << endl << "finalScore for Competitor " << competitorCounter << " is: " << finalScore <<endl;

competitorCounter += 1; //increment competitor counter by 1

}//end while

system("pause");

return 0;

} // end of main function``````

thanks!

Edited by shiny27: want to go to advanced editor

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by WaltP

You could start by telling us what the problem is..

You could start by telling us what the problem is..

oh sorry...

It will calculate the score for comepetitor one but not loop to correctly calculate the score for subsequent competitors.

Have I incorrectly nested my 'while' statements?

A quick inspection of the code reveals that your not resetting the variable judgeCounter back to zero.

A note on code indentation...Please use it.

``````#include <iostream>

using namespace std;

int main ()
{

int numberOfCompetitors;
float judgesScore;
float finalScore = 0.0;
int competitorCounter = 1;
int judgeCounter = 1;
float lowestScore = 6.0;
float highestScore = 0.0;

cout << "\n Welcome to the Diving Score program which \n determines the final score of each diver\n";
cout << " =========================================\n\n";

cout << "Please enter the number of competitors: ";
cin >> numberOfCompetitors;

while (competitorCounter <= numberOfCompetitors)
{
while (judgeCounter <= 5)
{
cout << "\nEnter the score for judge " << judgeCounter << ": ";
cin >> judgesScore;

if (judgesScore >= 0.0 && judgesScore <= 6.0)
{
finalScore += judgesScore;

if (judgesScore < lowestScore)
{
lowestScore = judgesScore;
}

if (judgesScore > highestScore)
{
highestScore = judgesScore;
}

judgeCounter += 1;
}
else
{
cout << "You have entered an incorrect value.\nPlease enter a value between 0.0 and 6.0...\n\n";
}
}
cout << "highestScore = " << highestScore;
cout << "\nloweastScore = " << lowestScore <<endl;

finalScore = (finalScore - highestScore - lowestScore) / 3;
cout << endl << "finalScore for Competitor " << competitorCounter << " is: " << finalScore <<endl;

competitorCounter += 1;
judgeCounter = 0;
}

return 0;
}``````

Edited by gerard4143: n/a

You do not re-enter your judging loop, because judgecounter is never <= 5 again,
After you increment your competitor counter, reset judgeCounter.

``````competitorCounter += 1; //increment competitor counter by 1
judgeCounter=1;``````

Too fast for me gerard!:'( that damn indentation bled my eyes and I had to take a break!

Edited by Akill10: n/a

That was it alright... I needed to reset the judgeCounter and score values.

Thanks you so much!

A quick inspection of the code reveals that your not resetting the variable judgeCounter back to zero.

A note on code indentation...Please use it.

Yes, but don't use gerard's formatting. Indent 4 spaces, not a full TAB. TABs make the code hard to read.

Your IDE can be set to convert the TAB to 4 spaces so the code looks good no matter where you post it.

IOW, TABs are evil. SPACEs are wonderful. :icon_wink:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.