Problem with looping back to start of the loop

Jun 29, 2015 at 12:23pm
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 Jun 29, 2015 at 12:24pm
Jun 29, 2015 at 12:29pm
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 Jun 29, 2015 at 12:33pm
Jun 29, 2015 at 12:31pm
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
Jun 29, 2015 at 12:32pm
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.
Jun 29, 2015 at 1:32pm
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
    }
}
Jun 29, 2015 at 1:44pm
I clearly mentioned that
programmer007 wrote:
using goto is a bad practice

but it would work for time being....
Last edited on Jun 29, 2015 at 1:45pm
Jun 29, 2015 at 5:22pm
@JewelCpp: I did? My logic is the same, just a different way of doing it.
Topic archived. No new replies allowed.