Help asking user if they want to run the program again [HELP]

So I've ran this properly and everything seems to be set up correctly.
However, I am still stumped on asking the user if they want to run the program again after either using case 1 or case 2, but it needs to have a cout statement clarifying they have quit this program if they have decided to quit like in case 3.

i was thinking about a while loop, I tried it and i don't think i did it correctly

my program is shown below without my mistake.


#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
// variables used for program
int choice;
int NumOfGymnasts = 0;
int getValidScore();

string name;

//code to welcome the user to the generator
cout << "Welcome to the Gymnastics Copetition Score Results!" << endl;


do
{
// start asking how many judges for the competition
cout << "Please pick from the following...\n" << endl;
cout << "1. 3 Judges \n" << endl;
cout << "2. 5 Judges \n" << endl;
cout << "3. Quit" << endl;
cin >> choice;

switch (choice)
{

// choice for 3 judges
case 1:
//narrowing down how many competitors and start calculation for scores
cout << "How many gymnasts are in the competition. Between 1-20 competitors\n" << endl;
cin >> NumOfGymnasts;

//entering a limit for how many gymnast in competition
if (NumOfGymnasts >= 1 && NumOfGymnasts <= 20)
{

//once clarified, varifying name and scores for gymnast in competition
for (int j =0; j < NumOfGymnasts; j++)
{

//asking user for gymnast name
cout << "Enter the gymnast name here...\n" << endl;
getline(cin, name);
cin >> name;

//identifiers for scores for three judges
double FIRST_SCORE, SECOND_SCORE, THIRD_SCORE;
double AVERAGE_SCORE;

//asking user judges score
cout << "Please score between 1-10. \n" << endl;
cout << "Enter first judge's score here...\n" << endl;
cin >> FIRST_SCORE;
FIRST_SCORE = getValidScore();
cout << "Enter second judge's score here...\n" << endl;
cin >> SECOND_SCORE;
SECOND_SCORE = getValidScore();
cout << "Enter third judge's score here...\n" << endl;
cin >> THIRD_SCORE;
THIRD_SCORE = getValidScore();

// output and calculations to provide average scoe for contestant
AVERAGE_SCORE = (FIRST_SCORE + SECOND_SCORE + THIRD_SCORE) / 3;
cout << fixed << setprecision(2);
cout << "Average score for contestant " << name << "is" << AVERAGE_SCORE << endl;

}

}

break;
case 2:{
// narrowing down how many competitors and start calculation for scores with 5 judges
cout << "How many gymnasts are in the competition. Pick between 1-50\n" << endl;
cin >> NumOfGymnasts;

// entering the limit for how many gymnast in competiton for 5 judges
if(NumOfGymnasts >= 1 && NumOfGymnasts <=50)
{
// once clarified, varifying name and scores for gymnasts in competition
for (int i = 0; i < NumOfGymnasts; i++)
{
//asking user for gymnast name
cout << "Enter the gymnast name here ...\n" << endl;
getline(cin, name);
cin >> name;

//identifiers for scores for five judges
double FIRST_SCORE, SECOND_SCORE, THIRD_SCORE, FOURTH_SCORE, FIFTH_SCORE;
double AVERAGE_SCORE;

//asking for user judges' score
cout << "Please score between 1-10.\n" << endl;
cout << "Enter first judge's score here...\n" << endl;
cin >> FIRST_SCORE;
FIRST_SCORE = getValidScore();
cout << "Enter second judge's score here...\n" << endl;
cin >> SECOND_SCORE;
SECOND_SCORE = getValidScore();
cout << "Enter third judge's score here...\n" << endl;
cin >> THIRD_SCORE;
THIRD_SCORE = getValidScore();
cout << "Enter fourth judge's score here...\n" << endl;
cin >> FOURTH_SCORE;
FOURTH_SCORE = getValidScore();
cout << "Enter fifth judge's score here...\n" << endl;
cin >> FIFTH_SCORE;
FIFTH_SCORE = getValidScore();

// output and calculations to provide aferage score for contestant
AVERAGE_SCORE = (FIRST_SCORE + SECOND_SCORE + THIRD_SCORE + FOURTH_SCORE + FIFTH_SCORE) / 5;
cout << fixed << setprecision(2);
cout << "Average score for contenstant " << name << "is " << AVERAGE_SCORE << endl;
}
break;

}
}
case 3:{
cout << " You have chosen to quit the progam. Thank you. \n Good bye." << endl;

continue;
break;
}
}
}

while(choice != 3);
}
int getValidScore()
{
int score;
while(true)
{
cin >> score ;
if (score > 1 || score > 10)
{
cout << "Invalid input. Must be between 1-10 for score. Please Re-enter here...\n" << endl;

}
else
{
break;
}
}

return score;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

while(true)
{
        // start asking how many judges for the competition
    cout << "Please pick from the following...\n" << endl;
    cout << "1. 3 Judges \n" << endl;
    cout << "2. 5 Judges \n" << endl;
    cout << "3. Quit" << endl;
    cin >> choice;
    
    switch(choice)
    {
        case 1: //whatever
        case 2: //whatever
        case 3: std::cout << "Exiting\n"; return 0;
    }
}


The code will automatically run the program again after selecting and finishing either 1 or 2, and will only terminate once the user hits 3. You don't have to ask the user if they want to run the program again. It will do it automatically.

Also, your code is very long and messy. Please use code tag and please split your switch case into functions. You should have a seperate function for threeJudges() and fiveJudges().
Last edited on
Topic archived. No new replies allowed.