Restart program

Hi guys,

This is my second day learning C++. How would i go about restarting this program? I would like to enter a grade number, receive the grade letter then have the program start over and ask for my grade number again...
im using C::B if that matters..

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
32
33
34
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x;

    cout << "Enter grade: ";
    cin >> x;

    if ( x >= 90 )
    {
        cout << "Your grade is A! \n";
    }
    else if ( x >= 80  )
    {
        cout << "Your grade is B! \n";
    }
    else if ( x >= 70  )
    {
        cout << "Your grade is C! \n";
    }
    else if ( x >= 60  )
    {
        cout << "Your grade is D! \n";
    }
    else
    {
        cout << "Your grade is F! \n";
    }

}
A do while loop is usually used for something like that.
http://www.cplusplus.com/doc/tutorial/control/
closed account (j3Rz8vqX)
Do you have a preference of loop types?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Pos test loop
do
{
    //Do something;
}while(x>0);//Repeat if true; proceed if false;

//Counter controlled loop
for(int i=0;i<3;i++)//Repeat loop 3 times
{
    //Do something;
}

//Pre Test loop
while(x>0)
{
    //Do something;
}
Love to offer some of my wizard skillz...

Pretty simple fix for this would be to use a loop. In this particular case, a while loop will be the loop of choice.

A while loop will continue until the loop condition is false. So what I will do is have a continue bool variable and set to true. After a grade is displayed to the console it will then ask the user to continue. If the user wants to continue then it will not change the bool variable but if the user wants to exit the program it will then change the variable and then exit the while loop which then will exit the program.


Here is some example code based on yours...


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
48
49
50
51
52
53
54
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x;
    bool continueYN = true;


     while (continueYN)
     {
           cout << "Enter grade: ";
            cin >> x;


        if ( x >= 90 )
       {
            cout << "Your grade is A! \n";
        }
       else if ( x >= 80  )
       {
        cout << "Your grade is B! \n";
       }
        else if ( x >= 70  )
       {
        cout << "Your grade is C! \n";
       }
     else if ( x >= 60  )
      {
         cout << "Your grade is D! \n";
      }
    else
      {
          cout << "Your grade is F! \n";
        }
       
       int YN;
       cout <<  "would you like to continue? Enter 1 for yes, 2 for no" << endl;
       cin >> YN;

      if (YN == 2)           //only checking to see if user entered 2 since this exits loop
             continueYN = false;    

      }



}







of course there are better ways to do this like using a do while loop and so on but I decided to keep it simple for you since you are new. Also, I cannot remember exactly if C++ uses bool or boolean for boolean variables so maybe try both until one works!

Happy programming!

erock
One addition to erock's suggestion would be to use break to end the loop instead of a Bool that remains in memory for the rest of that scope.

1
2
3
4
5
6
while(true) 
{
   //do stuff
   if(/*you want to exit*/) 
      break;
} 
Good work RadWayne. Never though to do it that way. Makes it even simpler.

erock
One addition to erock's suggestion would be to use break to end the loop instead of a Bool that remains in memory for the rest of that scope.

You mean after the loop ends?, how is that different? You still need to use a variable to trigger the break,

It could be done with any of the ways Dput mentioned, but a do/while loop (or game loop) is the better method for something like this since it will run at least once no matter what the flag is set at. IMO, using break for something like this is little different than using GOTO.
thank you guys. using a while loop worked.

ive been working with networks for 8 years and went back to school last year for IT. so far ive had to take a visual basic and javascript class and soon a java class. needless to say i feel i got bit by the programming bug.

on thing i noticed on the solution that erock 88 suggested is, howcome there is no open curly bracket on the second if 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
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x;
    bool continueYN = true;

    while (continueYN)
    {
    cout << "Enter grade: ";
    cin >> x;


    if ( x >= 90 )
    {
    cout << "Your grade is A! \n";
    }
    else if ( x >= 80  )
    {
    cout << "Your grade is B! \n";
    }
    else if ( x >= 70  )
    {
    cout << "Your grade is C! \n";
    }
    else if ( x >= 60  )
    {
    cout << "Your grade is D! \n";
    }
    else
    {
    cout << "Your grade is F! \n";
    }

    int YN;

    cout <<  "Would you like to Enter new grade? 1 for Yes, 2 for No" << endl;
    cin >> YN;

    if (YN == 2)    //************* NO OPEN CURLY BRACKET ************

    continueYN = false;
    }

}


normally with javascript you would open a set of curly brackets after an if statement.
closed account (j3Rz8vqX)
An if statement will auto encapsulate the next line of code if no brackets were defined, else use curly brackets to encapsulate procedures of an amount greater than one..

Example:
if(true)
---Do this - only if true;

//versus

if(true)
{
---Do all of this till the next '}'
---And this stuff too, before the next '}'
}
Topic archived. No new replies allowed.