Re start a program

Practicing my if/else functions with a simple number program that asks the user to input and carry out functions to a chosen number. If the user inputs the wrong answer or an invalid number I would like the code to be able to be restarted. Is there any way of doing this?

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
55
56
57
58
59
60
61
62
63

#include <iostream>

using namespace std;

void correct() //Function to display text if correct answer inputted
{
    cout << "Correct\n" << endl;
}

void incorrect()
{
    cout << "Incorrect, press r to start again" << endl; //Function to display text if incorrect answer inputted
    //some code to make program restart if r is pressed
}

int main()
{
    int x;
    cout << "Enter am integer between 1 and 10 (inclusive)" << endl;
    cin >> x;

    if (x > 0 && x < 11)
    {
        cout << "Thank you\n" << endl;
    }
    else
    {
        cout << "Number invalid, press r to start again\n" << endl;
        return 0;
    }

    cout << "Please double your number" << endl;
    int y;
    cin >> y;

    if (y == x*2)
    {
        correct();
    }
    else
    {
        incorrect();
        return 0; //Ends program
    }

    cout << "Please add 2 to your number" << endl;
    int z;
    cin >> z;

    if (z == y + 2)
    {
        correct();
    }
    else
    {
        incorrect();
        return 0; //Ends program
    }
    return 0;
}

The whole main function?

You could do something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    while (true) // or whatever other condition, if you want to limit it)
    {
        //stuff you already have
        ...
        else
        {
            incorrect();
            //instead of returning...
            continue; //makes it start back at the top of the while loop
        }
    }
}

Last edited on
If I do that though it says

 
error: 'else' without a previous 'if'
And would something like this work in the while loop?

 
while (y == x*2 && z == y + 2)
The ellipsis was supposed to mean the rest of the code.

As in, wrap everything you currently have in main() around a while loop, but change the returns to continues.

1
2
3
4
5
6
7
8
9
10
    if (y == x*2)
    {
        correct();
    }
    else
    {
        incorrect();
        continue;
    }
    //do this for the other places you want to "restart" instead of return as well 


Edit: If you want a condition in your while loop, it would be a condition that, if evaluated to false, would make the program end. You could have a "num_tries" that gets incremented, and it num_tries goes above 10 or something, it ends the loop.

Just to clarify...
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
int main()
{
    while (true) {
        int x;
        cout << "Enter am integer between 1 and 10 (inclusive)" << endl;
        cin >> x;

        if (x > 0 && x < 11)
            cout << "Thank you\n" << endl;
        else
        {
            cout << "Number invalid, press r to start again\n" << endl;
            continue; //restarts to beginning of while loop
        }

        cout << "Please double your number" << endl;
        int y;
        cin >> y;

       if (y == x*2)
        {
           correct();
        }
        else
        {
            incorrect();
            continue;  //restarts to beginning of while loop
        }

        cout << "Please add 2 to your number" << endl;
        int z;
        cin >> z;

        if (z == y + 2)
        {
            correct();
        }
        else
        {
            incorrect();
            continue;  //restarts to beginning of while loop
        }
        //If this point is reached, it means the person followed the directions correctly.
        break; //ends loop, allows program to return and exit.
    }
    return 0;
}
Last edited on
Right that all works now (I included a break; after the last if to end the program if the user gets to the last function correctly), thanks for the help. Just out of interest (not that you'd need/want to do this but) would it be possible to make the program restart if the user inputs a character?
Last edited on
You could have something like

1
2
3
4
5
6
7
8
9
10
11
12
13
if (whatever condition)
    correct();
else
{
    incorrect();
    cout << "Enter y to restart: ";
    char c;
    cin >> c;
    if (c == 'y')
        continue;
    else
        return 1;
}

There's probably a better way to structure the program but it works.
Last edited on
Ahh thank you very much. In what way could it be better? I'm a total beginner so if you could suggest a general solution kind of thing I could work through to help me improve that would be fab, if not maybe some specific pointers?
you should use do while loop here.
Here's the fix code
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>

using namespace std;
int z;
void correct() //Function to display text if correct answer inputted
{
    cout << "Correct\n" << endl;
}

void incorrect()
{
    cout << "Incorrect, press r to start again" << endl; //Function to display text if incorrect answer inputted
    //some code to make program restart if r is pressed
}

int main()
{
    int x;
    do{

cout << "Enter am integer between 1 and 10 (inclusive)" << endl;
    cin >> x;
} 
while(x<1 || x>10); // here's your problem i changed it into do while loop if the input
                              // is wrong the program will be restarted
    cout << "Please double your number" << endl;
    int y;
    cin >> y;

    if (y == x*2)
    {
        correct();
    }
    else
    {
        incorrect();
        return 0; //Ends program
    }

  cout << "Please add 2 to your number" << endl;
    int z;
    cin >> z;

    if (z == y + 2)
    {
        correct();
    }
    else
    {
        incorrect();
        return 0; //Ends program
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.