while loop fails to run

Pages: 123
Not sure what is stopping this while loop from running

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
#include <iostream>

using namespace std;

int main()
{
    string name;
    int score, total = 0, x, quit = 0;


    while (quit = 0);
{
         cout << " Enter student name: ";
         cin >> name;
    for ( x=0; x<5; x++)
    {
         cout << " Enter score: ";
         cin >> score;
         score += total;

         if (score < 1 || score > 10)
         {
             cout << " Re-enter score (between 1 - 10): ";
             cin >> score;
         }

     }
    cout << " Enter 1 to quit or 0 to enter another: ";
    cin >> quit;
}

    cout << total << endl;



    return 0;
}
line 11, look closely...
exactly!

[edit]
@closed account
you didn't had to delete your post just because I also wrote a similar answer at the same time (just a few seconds before you I assume)... More the answers... better the forum :)
Last edited on
closed account (48T7M4Gy)
line 19 total += score; ??

 
Enter student name: Smith
 Enter score: 2
 Enter score: 6
 Enter score: 7
 Enter score: 9
 Enter score: 2
 Enter 1 to quit or 0 to enter another: 1
26
 
Exit code: 0 (normal program termination)


Also while ( quit != 0) or maybe while( quit == 0) which one??
Last edited on
Hi,

while (quit = 0)


When you compare, don't use assignment operator (=), use comparison operator (==)

while (quit == 0)
Does that help you? :)
yeah I knew it was something simple but can you help with that line 19 what is wrong there?
What happens in line 19?

Ok,
score += total;
And :
total += score;

What makes the logic?
Last edited on
closed account (48T7M4Gy)
I'm assuming you want to add the scores up. So you input a score and add it to the total. total = total + score, or if you like total += score, it's the same.

Then you go round the loop agin and get another score and add that one to the total. That is if my assumption is right that you just want to get the total of the scores.
Last edited on
@mjah

the if statement will help if it is executed before summing up the total

@closed account

yes it does... :D
ok ty yes its 5 scores for each contestant so I think it works nice
closed account (48T7M4Gy)
So if you always want to add up 5 scores every time you don't need to ask to quit.

You will only need the for loop in that case.
maybe the OP is trying to create an 2d array and wants to input the data of more than one student...
closed account (48T7M4Gy)
Could be.
OK, maybe i was not clear enough the code runs but the loop does not repeat even now with quit == 0 it is still the same
and yes it will be for undefined number of students
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while (quit == 0);
{
         cout << " Enter student name: ";
         cin >> name;
    for ( x=0; x<5; x++)
    {
         cout << " Enter score: ";
         cin >> score;
         total += score;
         if (score < 1 || score > 10)
         {
             cout << " Re-enter score (between 1 - 10): ";
             cin >> score;
         }
     }
    cout << " Enter 1 to quit or 0 to enter another: ";
    cin >> quit;
}
    cout << total << endl;
    return 0;
}
Last edited on
You need to remove return 0;. Does that help?
no
same run it
Pages: 123