while loop fails to run

Pages: 123
closed account wrote:
You need to remove return 0;.

No. The return 0; is in the correct place in the OP.

The problem is the while statement.
 
while (quit == 0);  // <- Note the semicolon 

The semicolon terminates the while statement, effectively doing nothing. The following code executes exactly once since it is not under the control of the while statement.
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
#include <iostream>

using namespace std;
int main()
{
    string name;
    int score, total = 0, quit = 0;
    while (quit == 0)
{
         total = 0;
         cout << " Enter student name: ";
         cin >> name;

    for (int x=0; x<5; x++)
    {
         cout << " Enter score (" << x + 1 << ") : ";
         cin >> score;
        if (score < 1 || score > 10)
         {
             cout << " Re-enter score (between 1 - 10)\n\n"; x -= 1; continue;
         }
         total += score;
     }
    cout << "Student " << name << endl;
    cout << "Total score : " << total << endl;
    cout << "Average score : " << ((double)total / 5.0) << endl << endl;
 
    cout << " Enter 1 to quit or 0 to enter another: ";
    cin >> quit; quit = ((quit == 1) ? 1 : 0);
}
   return 0;
}


Is that what you want?
Last edited on
Edit : I edited one line. Now it is perfect.
I had to edit also it still did not loop now it is correct thank you
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
#include <iostream>

using namespace std;
int main()
{
    string name;
    int score, total = 0, quit = 0;
    while (quit == 0)
{
         total = 0;
         cout << " Enter student name: ";
         cin >> name;

    for (int x=0; x<5; x++)
    {
         cout << " Enter score (" << x + 1 << ") : ";
         cin >> score;
        if (score < 1 || score > 10)
         {
             cout << " Re-enter score (between 1 - 10)\n\n"; x -= 1; continue;
         }
         total += score;
     }
    cout << "Student " << name << endl;
    cout << "Total score : " << total << endl << endl;

    cout << " Enter 1 to quit or 0 to enter another: ";
    cin >> quit;
}
   return 0;
}
Glad it helped :)
Another question don't kill me! How would I keep each separate score value without combining into a total to drop lowest and highest to get average. Just asking how I would do this I was thinking array but cant get it right just an idea I do not want the answer given to me?
My solution updated. Average score display added.
OK, I have to use a function to calculate and drop lowest / highest which I have used before just carrying the values is what I am asking not sure how to do this?
> OK, I have to use a function to calculate and drop lowest / highest which I have used before just carrying the values is what I am asking not sure how to do this?

Can you give us some examples please?
well function would go
void calcAvg(int score1, int avg, int score2,) and so on
{
cout << avg << endl;
}

just something like that it will yake me some time to get th formula right you know but I will need each value to call and drop lowest / highest. I was looking for a way to not cin score1 , score2, ... I am sure there is a way?
want my code to be cleaner is all
> drop lowest / highest
What do you mean by that?
oh the lowest and highest score will be dropped in the function and then averaged and compared to other contestants. hold on I will give you what I have
1. Input the contestant’s first name followed by the 5 judges’ scores. You do not know how many contestants there are. Design the loop so the loop terminates when you are finished entering contestants.
2. Input validation: Do not accept a judge’s score that is less than 1 or greater than 10. As each score is entered send the score to a function to test the score’s validity.
3. Use function calcAvgScore that has the contestant’s 5 scores as input parameters
a. returns the average score for that contestant.
b. Calls two functions, findLowest and findHighest which both accept the 5 scores as input parameters and return the lowest and highest scores, respectively.
4. Do not use global variables. All variables used in the functions must be passed as parameters or declared locally.
5. At the end of the program, display the winner and winning score (rounded to 2 decimal places).

I am really not asking you to do this assignment for me I just am not sure how to store the scores in a clean way.
Sorry, but I can't help you further :-P
Remember the topic is "while loop fails to run"
Okay I do need help lol I am not doing so good here.
can I get help please? I am not doing as well as I expected
closed account (48T7M4Gy)
Store the scores in an array.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194087/
Multiple posting of the same problem is bad practice. Choose one and close the other.
Last edited on
how to close and not same problem just same program?
closed account (48T7M4Gy)
Green tick one as complete. If you want to argue about it then tick both!
Pages: 123