Hi everyone, my program should read in a number of judges and a number of participants and then ask for a score from each judge, calculate and output the winner. When i try to check for wrong input, it cancels out my program. Can anyone tell me what i did wrong?
#include <iostream>
#include <iomanip>
usingnamespace std;
int main () {
int numJudges, celebNum;
double highScore;
bool validInput;
string celebName[15];
double celebscore[15] = {0};
double maxval = {0};
string maxname;
do {
cout << "\a\tEnter the number of judges: ";
cin >> numJudges;
if (numJudges >= 2 && numJudges <= 6)
validInput = true;
else
cout << "\n" << numJudges << " is an invalid number of judges.\n"
<< "The number of judges must be between 2 and 6.\n\n";
}
while (validInput == false);
do {
cout << "\a\tEnter the number of celebrities: ";
cin >> celebNum;
if (celebNum >= 3 && celebNum <= 15)
validInput = true;
else
cout << "\n" << celebNum << " is an invalid number of celebrities.\n"
<< "The number of celebrities must be between 10 and 15.\n\n";
}
while (validInput == false);
int curcelebnumber = {-1};
for (int i = 1; i<= celebNum; i++){
double totalScore, score;
string name;
cout << "\n\a\tPlease enter the celeb name: ";
cin.get();
getline(cin, name);
celebName[i-1] = name;
totalScore = 0;
curcelebnumber++;
do {
for (int ia = 1; ia <= numJudges; ia++){
cout << "\t\tEnter the score: \t";
cin >> score; //increase score
totalScore += score;
}
celebscore[curcelebnumber] = totalScore;
if (score >= 0 && score <= 6)
validInput = true;
else
cout << "\n" << score << " is an invalid number of judges.\n"
<< "The score must be between 0 and 6.\n\n";
}
while (validInput == false);
}
for (int i = 0; i <= celebNum -1 ; i++){
cout << "The Total Score For " << celebName[i] << " is: " << setiosflags(ios::fixed) << setprecision(1) << celebscore[i] << endl << "\n";
cout << "The Average Score For " << celebName[i] << " is: " << setiosflags(ios::fixed) << setprecision (1) << celebscore[i]/numJudges << endl <<"\n";
}
for (int i = 0; i <= celebNum -1 ; i++){
if (celebscore[i] > maxval)
{
maxval = celebscore[i];
maxname = celebName[i];
}
}
cout << "And the winner is... \n" << maxname << " \n" << "Score:" << maxval / numJudges;
return 0;
}
Between lines 26 and 27 you never reset validInput to false.
By the way, the preferred way to validate input does not require using validInput:
1 2 3 4 5 6 7
while((cout << "\a\tEnter the number of judges: ")
&& (cin >> numJudges)
&& (numJudges < 2 || numJudges > 6))
{
cout << "\n" << numJudges << " is an invalid number of judges.\n"
<< "The number of judges must be between 2 and 6.\n\n";
}
The only way the previous do-while loop can end is if validInput gets set to true. This means that the next do-while loop starts with validInput already set to true, which I don't think you intended as that was not the case for the first do-while loop.
EffyPT wrote:
Also i found that they way you tried to do it there doesnt work for me without a for loop inside the do while loop
When i try to implement your do while loop on my code it keeps telling me that it expects a 'while' before '{' token
1 2 3 4 5 6 7 8 9
do {
while((cout << "\a\tEnter the number of judges: ")
&& (cin >> numJudges)
&& (numJudges < 2 || numJudges > 6))
{
cout << "\n" << numJudges << " is an invalid number of judges.\n"
<< "The number of judges must be between 2 and 6.\n\n";
}
}
Im noob at programming and i assumed it needed a do anywhere tou have a while statement sorry.
So a got rid of the do {} and the programs runs fine until i enter the right number for the celebrities, then it doesn't move from there and it doesn't give an error message either
while((cout << "\a\tEnter the number of judges: ")
&& (cin >> numJudges)
&& (numJudges < 2 || numJudges > 6))
{
cout << "\n" << numJudges << " is an invalid number of judges.\n"
<< "The number of judges must be between 2 and 6.\n\n";
}
while((cout << "\a\tEnter the number of celebrities: ")
&& (cin >> celebNum)
&& (celebNum < 2 || celebNum > 6))
{
cout << "\n" << celebNum << " is an invalid number of judges.\n"
<< "The number of judges must be between 2 and 6.\n\n";
}
while (validInput == false);
int curcelebnumber = {-1};
for (int i = 1; i<= celebNum; i++){
double totalScore, score;
string name;
cout << "\n\a\tPlease enter the celeb name: ";
cin.get();
getline(cin, name);
celebName[i-1] = name;
totalScore = 0;
curcelebnumber++;
while((cout << "\a\tEnter the number of judges: ")
&& (cin >> numJudges)
&& (numJudges < 2 || numJudges > 6))
{
cout << "\n" << numJudges << " is an invalid number of judges.\n"
<< "The number of judges must be between 2 and 6.\n\n";
}
do {
for (int ia = 1; ia <= numJudges; ia++){
cout << "\t\tEnter the score: \t";
cin >> score; //increase score
totalScore += score;
}
celebscore[curcelebnumber] = totalScore;
if (score >= 0 && score <= 6)
validInput = true;
else
cout << "\n" << score << " is an invalid number of judges.\n"
<< "The score must be between 0 and 6.\n\n";
}
while (validInput == false);
}
#include <iostream>
#include <iomanip>
usingnamespace std;
int main () {
int numJudges, celebNum;
double highScore;
bool validInput=false;
string celebName[15];
double celebscore[15] = {0};
double maxval = {0};
string maxname;
do {
cout << "\tEnter the number of judges: ";
cin >> numJudges;
if (numJudges >= 2 && numJudges <= 6)
validInput = true;
else
cout << "\n" << numJudges << " is an invalid number of judges.\n"
<< "The number of judges must be between 2 and 6.\n\n";
}
while (validInput == false);
do {
validInput=false;
cout << "\tEnter the number of celebrities: ";
cin >> celebNum;
if (celebNum >= 3 && celebNum <= 15)
validInput = true;
else
cout << "\n" << celebNum << " is an invalid number of celebrities.\n"
<< "The number of celebrities must be between 3 and 15.\n\n";
}
while (validInput == false);
int curcelebnumber={-1};
for (int i = 1; i<= celebNum; i++){
double totalScore, score;
string name;
cout << "\n\tPlease one celebrities name: ";
cin.get();
getline(cin, name);
celebName[i-1] = name;
totalScore = 0;
curcelebnumber++;
for (int ia = 1; ia <= numJudges; ia++){
cout << "\t\tEnter the score: \t";
cin >> score; ///increase score
totalScore += score;
celebscore[curcelebnumber] = totalScore;
if (score >= 0 && score <=10)
validInput = true;
else
cout << "\n" << score << " is an invalid number of judges.\n"
<< "The score must be between 0 and 10.\n\n";}
}
for (int i = 0; i <= celebNum -1 ; i++){
cout << "The Total Score For " << celebName[i] << " is: " << setiosflags(ios::fixed) << setprecision(1) << celebscore[i] << endl << "\n";
cout << "The Average Score For " << celebName[i] << " is: " << setiosflags(ios::fixed) << setprecision (1) << celebscore[i]/numJudges << endl <<"\n";
}
for (int i = 0; i <= celebNum -1 ; i++){
if (celebscore[i] > maxval)
{ maxval = celebscore[i];
maxname = celebName[i];}}
cout << "And the winner is... \n" << maxname << " \n" << "Score:" << maxval / numJudges;
return 0;}