Favorite number help

i came up with this and it works but i would like it not to close when the user enters the wrong number but i dont know how to get it to restart
i made it in C++ express 2008

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Include the iostream library

#include <iostream>



//Use the standard namespace

using namespace std;



void main()
{

    // Declare the variables
	int secretNumber = 4;
	int Dog = 0;
	int Number = 1;
	int Cow = 2;
	int Sheep = 3;
	int Apple = 5;
	int Car = 6;
	int Pizza = 7;
	int Pig = 8;
	int Horse = 9;
	int userguess;

    // Give instructions

    cout << "Hello can you guess My favorite number?" << endl;

    //Input from user:

    cin >> userguess;

	//Print The Result
    if (userguess == secretNumber)
    {
        cout << "That is amazingly correct!"<<endl;
    }
	if (userguess == Dog)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Pig)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Number)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Horse)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Car)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Cow)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Apple)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Pizza)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	if (userguess == Sheep)
    {
        cout << "That is Not My Favorite Number Guess Again Next Time"<<endl;
    }
	
	system ("Pause");

}


i edit it a little
Last edited on
closed account (zb0S216C)
Please, use code tags. Press the button that looks like [<>] on the right-hand side.

Before proceeding, main() should return int.

Now, to your problem. To make a program repeat itself without restarting the program, you need a loop. There's 3 types:

1) for[1]
2) while[2]
3) do...while[3]

References:
[1] http://www.cplusplus.com/doc/tutorial/control/#for
[2] http://www.cplusplus.com/doc/tutorial/control/#while
[3] http://www.cplusplus.com/doc/tutorial/control/#do


Wazzak
Topic archived. No new replies allowed.