Problem with looping back to start of the loop

How should I edit my program so if I enter a # bigger than 80, I have to enter another number ("input") and if this "input" is equal to/less than 80, it gives me one of the 3 following statements?

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
using namespace std;

int main( )
{
   int Input;
   int x;

       cerr << "Input:" << endl;
       cin >> Input;

       if (Input/2 <= 29.5)
       {
           cout << "You got a F" << endl;
       }

       if (Input/2 > 29.5 && Input/2 <= 34.5)
       {
           cout << "You got a D" << endl;
       }

       if (Input > 69 && Input <= 79)
       {
           cout << "You got a C" << endl;
       }

       if (Input > 80)
       {
           cout << "The grade you entered cant be above 80, try again" << endl;
           cin >> Input;
       }



return 0;

}


In the last "if" loop, I expected the code within the loop to loop back to the beginning of the program, but it doesn't.
Last edited on
just to make the work easy use a goto 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
33
34
35
36
37
using namespace std;

int main( )
{
   int Input;
   int x;
again:  //label is here
       cerr << "Input:" << endl;
       cin >> Input;

       if (Input/2 <= 29.5)
       {
           cout << "You got a F" << endl;
       }

       if (Input/2 > 29.5 && Input/2 <= 34.5)
       {
           cout << "You got a D" << endl;
       }

       if (Input > 69 && Input <= 79)
       {
           cout << "You got a C" << endl;
       }

       if (Input > 80)
       {
           cout << "The grade you entered cant be above 80, try again" << endl;
           goto again;// if the value is greater than 80 it will jump to the label
       }



return 0;

}


PS: using goto is a bad practice. You could try while loop
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
I expected the code within the loop to loop back to the beginning of the program
Why? Nothing in your code says "return back to the beginning". Add a loop: http://www.cplusplus.com/doc/tutorial/control/#loops
How about a while-loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

    bool bGettingInput = true;

    while( bGettingInput )
        {
        /*    PUT YOUR CODE HERE:    */

        /*    MODIFY YOUR LAST IF:    */

        if( Input > 80 )
            cout << "The grade you entered can't be above 80, try again" << endl;
        else
            bGettingInput = false;

        }    /*    while( bGettingInput )    */



This should keep you in a loop until the user choose the correct number.
Please don't do like programmer007 gave as answer:
just to make the work easy use a goto statement

don't ever use goto. They are not sign of a good programming. Sometimes they can be very tempting but don't use them.

As MiiNiPaa said
Nothing in your code says "return back to the beginning"

the main function is executed only once. It does not re-loop by itself.

As boothkeeper said use a while loop instead.

but he didn't correctlly use the if/else conditions:
1
2
3
4
5
6
7
8
9
10
11
12
bool validGrade=false
while(!validGrade)
{
    validGrade=true;
    //read the input here
    //do the if conditions here
    if(Input>80)
    {
         cout << "The grade you entered can't be above 80, try again" << endl;
         validGrade=false;  //to return to the begining
    }
}
I clearly mentioned that
programmer007 wrote:
using goto is a bad practice

but it would work for time being....
Last edited on
@JewelCpp: I did? My logic is the same, just a different way of doing it.
Topic archived. No new replies allowed.