Not looping when out of parameter

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int score1, score2, score3; // Three scores
   double average;             // Average score
   char again;                 // To hold Y or N input

   do
   {
      // Get three scores.
      cout << "Enter your 3 test scores and I will average them: \n";
      cin >> score1;
      cin >> score2;
      cin >> score3;
        if (score1 < 0 || score2 < 0 || score3 < 0 )
        {
            cout << "Invalid input, score cannot be less than zero.\n";
            continue;  
        }
      
      // Calculate and display the average.
      average = (score1 + score2 + score3) / 3.0;
      cout << "The average is " << average << ".\n";
      
      // Does the user want to average another set?
      cout << "Do you want to average another set of scores? (Y/N) ";
      cin >> again;     //If user enters anything other the y,Y,n,N the program will exit the loop and continue.
      if (again == 'n' || again == 'N')
      {
            cout << "Press enter to close.\n";   //pauses the program to view results before closing.
            cin.ignore();
            cin.get();
        }      
   } while (again == 'Y' || again == 'y');
   
   return 0;
}

this part.
1
2
3
4
5
6
7
8
9
10
// Get three scores.
      cout << "Enter your 3 test scores and I will average them: \n";
      cin >> score1;
      cin >> score2;
      cin >> score3;
        if (score1 < 0 || score2 < 0 || score3 < 0 )
        {
            cout << "Invalid input, score cannot be less than zero.\n";
            continue;  
        }

neither continue nor break will allow the program to loop to the beginning, it only closes..
Last edited on
Got my answer... Had to do a nest do while loop.

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int score1, score2, score3; // Three scores
   double average;             // Average score
   char again;                 // To hold Y or N input

   do
   {
        do
        {
      // Get three scores.
      cout << "Enter 3 test scores and I will average them: \n";
      cout << "What is your first test score?\n";
      cin >> score1;
      cout << "What is your second test score?\n";
      cin >> score2;
      cout << "What is your third test score?\n";
      cin >> score3;
      
            if (score1 < 0 || score2 < 0 || score3 < 0 )
                {
                    cout << "Invalid input, score cannot be less than zero.\n\n";  
                }
        }while (score1 < 0 || score2 < 0 || score3 < 0 );
        
      // Calculate and display the average.
      average = (score1 + score2 + score3) / 3.0;
      cout << "The average is " << average << ".\n";
      
      // Does the user want to average another set?
      cout << "Do you want to average another set of scores? (Y/N) ";
      cin >> again;     //If user enters anything other the y,Y,n,N the program will exit the loop and continue.
      if (again == 'n' || again == 'N')
      {
            cout << "Press enter to close.\n";   //pauses the program to view results before closing.
            cin.ignore();
            cin.get();
        }      
   } while (again == 'Y' || again == 'y');
   
   return 0;
}
Topic archived. No new replies allowed.