Hi...
I want to insert a variable finalScore into an array competitorsArray[competitorCounter] . I can't find any instructions anywhere on how to do this, so I'm wondering if it's even possible.
Also, how do I output an error message if the user enters a number that I then store into an array if the number is not within certain parameters, ie the number should be between 0.0 and 6.0 and only be in increments of 0.1, only one decimal place?
I've included my original code with comments as to what I would like to do and what I've already tried.
Thanks in advance for any help. btw I'm a newbie at C++ :)
/*##########################################################################
### 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 <iomanip>
#include <iostream>
using namespace std;
int main (){
char closeProgramOption = 'y'; // Declare variable y that will be used in close/restart program option
while(closeProgramOption=='y'){
int numberOfCompetitors; // Number of competitors
float judgesScore; // Score awarded by each individual judge
float finalScore = 0.0; // Diver's final score, initialised to 0.0
int competitorCounter = 1; // Counts competitors, initialised to 1
int judgeCounter = 1; // Counts judges, initialised to 1
float lowestScore = 6.0; // Lowest score awarded to diver, initialised to 6.0
float highestScore = 0.0; // Highest score awarded to diver, initialised to 0.0
int i; // control variable for loop
float insert; // temporary variable to hold element to insert
/*---------------------
Display welcome message
----------------------*/
cout << "\n ******************************************";
cout << "\n * *";
cout << "\n * Welcome to the Diving Score for the *\n";
cout << " * London, 2012 Olympic Games *";
cout << "\n * *";
cout << "\n ******************************************\n\n";
/*------------------------------------------
Asks user to input the number of competitors
-------------------------------------------*/
cout << "Please enter the number of competitors: ";
cin >> numberOfCompetitors; // Store number of competitors
cout << endl;
/*-------------------------------------------------------------------------------
Processing phase:
-----------------
This phase takes the input from each of 5 judges for each individual competitor,
calculates the competitor's final score by disregarding the highest and lowest
score awarded and obtaining the average of the 3 remaining scores.
--------------------------------------------------------------------------------*/
float competitorsArray[numberOfCompetitors]; // Declare array to hold the scores
//from all the competitors
while (competitorCounter <= numberOfCompetitors){
cout << fixed << setprecision ( 1 ); // sets output of scores to one decimal place
cout << "Scores for Competitor: " << competitorCounter << endl;
while (judgeCounter <= 5){
cout << "Enter the score from judge no. " << judgeCounter << ": "; // Prompts user to enter judges'score
cin >> judgesScore;
// Determine if judge's score awarded is within acceptable range, i.e. 0.0 to 6.0:
if (judgesScore >= 0.0 && judgesScore <= 6.0){
// Add score to competitor's final score
finalScore += judgesScore;
// Determines lowest score awarded to competitor
if (judgesScore < lowestScore){
lowestScore = judgesScore;
}
// Determines highest score awarded to competitor
if (judgesScore > highestScore){
highestScore = judgesScore;
}
// Increment judgeCounter by 1
judgeCounter += 1;
}
else{
// Display error message if incorrect value for judge's score is entered
cout << "You have entered an incorrect value.\nPlease enter a value between 0.0 and 6.0...\n\n";
}
} // end while
// Display Highest and Lowest Score
cout << "highestScore = " << highestScore << "\nlowestScore = " << lowestScore << endl;
// Calculate competitor's final score
finalScore = (finalScore - highestScore - lowestScore) / 3;
// Display competitor's final score
cout << endl << "The final score for Competitor " << competitorCounter << " is: " << finalScore << endl << endl;
/*This is the code I also have using an array but I'm unable to display an error message if the user enters an incorrect value for judges score
cout << "Scores for Competitor: " << competitorCounter << endl;
//input scores from judges
for ( i=0; i<5; i++){
cout << "Score from Judge No. " << i+1 << ": ";
cin >> setprecision ( 1 ) >> judgeScore[i];
}
//output scores
for (i = 0; i<5; i++){
cout << fixed << setprecision ( 1 );
cout << "Score from Judge: " <<i+1 << ": " << judgeScore[i] << "\n";
}
// sort judges score into ascending order
// insertion sort
// loop over the elements of the array
for (int next=1; next<arraySize; next++){
insert = judgeScore[next];
int moveItem = next;
while ((moveItem >0) && (judgeScore[moveItem -1] > insert)){
judgeScore[moveItem] = judgeScore[moveItem - 1];
moveItem--;
} // end while
judgeScore[moveItem] = insert;
} // end for
cout << endl;
// Display Highest and Lowest Score
cout << "highestScore = " << judgeScore[4] << "\nlowestScore = " << judgeScore[0] << endl;
//Calculate final score
finalScore = (judgeScore[1] + judgeScore[2] + judgeScore[3]) / 3;
// Display competitor's final score:
cout << endl << "The final score for Competitor " << competitorCounter << " is: " << finalScore << endl << endl;
cout << endl << endl; */
/* here I want to enter final score into an array that will diplay all the final scores from all competitors at the end of the program, which I can sort and output in ascending or descending order*/
// Increment competitorCounter by 1
competitorCounter += 1;
// Reset counters to re-enter processing loop
judgeCounter = 1;
lowestScore = 6.0;
highestScore = 0.0;
finalScore = 0.0;
} //end while
// End program or restart
cout <<"\n Do you want to continue:? \n Press (Y) to restart OR any other key to Exit ";
cin >> closeProgramOption;
} // end while
return 0;
} // end main
I hope all this makes sense :)