do while loop not working properly

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iomanip>


using namespace 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";
}
Last edited on
What do you mean by reset validInput to false? sorry but i'm a bit noob at programming yet.

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
EffyPT wrote:
What do you mean by reset validInput to false?
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
Could you elaborate? It works for me.
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";
}
}
Why did you add do {} to it? While loops and do-while loops have different syntax.
Last edited on
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    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);
}
i found the problem sorry
 
while (validInput == false);


that line shouldnt be there, thank you for your help
I found other problems in your code
code fixes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <iomanip>
using namespace 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;}

You'll know them when you see'm
Topic archived. No new replies allowed.