Guessing game. Prgram restart troubles.

Hello im new too C++
Im making a number guessing program and it works fine but im trying to figuire out a way to restart it after the player has guess once.

This is what i have so far (ignore the slashes)

//////////////////////////////////////////////////////////////////////////////
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>;

using namespace std;

int main()
	
{
	system ("COLOR 03");
	system ("TITLE The Guessing Game! By: Jessie Keeton");
int i = 46;
int j = 0;
int k = 0;

cout << "**| What Is The Number Im Thinking Of?  |** " << endl;
cin >> j;

if (i == j)
{
	
	system ("COLOR 12");
	cout << " Correct! " << endl;
}
else if(j > i)
{
	cout << " Wrong ansewer Too high! " << endl;
	system ("COLOR 04");
}
else
{
	system ("COLOR 04");
	cout << " Wrong Number Too low! " << endl; 
}
break;
cout << "\n\n";
cout << " Try Again Y/N? \n" << endl;
char k;
cin >> k;
if (k == y)
{
main();
{
cin.get();
}
{
	cout << " Very well the game will restart soon! " << endl; 
}
system ("pause");
}

//////////////////////////////////////////////////////////////////////////////
Last edited on
You have quite a mess in lines 38 to 46

You should use a do-while loop:
1
2
3
4
5
6
7
8
int main()
{
    do
    {
        // ...
    }
    while ( condition ); // it will repeat everything inside the do if condition is true
}
http://www.cplusplus.com/doc/tutorial/control/#do
Im confused lol
i understand that if the condition is met ( for example if (k == y); is press then the game restarts)

but what do i put under the do to make the game restart? do i put main()?
do i put main()?


No. You must never call main(). Restarting a function by calling it is a bad idea in general (unless the function is designed to be recursive, but that's another topic).

"Restarting" code is the same as "looping" code. Anything you want to loop/repeat/restart goes inside the do block.
Last edited on
Recursive function calls to main are evil, use this instead:
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
#include <iostream>;

using namespace std;

int main()
	
{
        do
        {
	        system ("COLOR 03");
	        system ("TITLE The Guessing Game! By: Jessie Keeton");
                int i = 46;
                int j = 0;
                int k = 0;
                cout << "**| What Is The Number Im Thinking Of?  |** " << endl;
                cin >> j;
                if (i == j)
                {
	                system ("COLOR 12");
        	        cout << " Correct! " << endl;
                }
                else if(j > i)
                {
	                cout << " Wrong ansewer Too high! " << endl;
	                system ("COLOR 04");
                }
                else
                {
        	        system ("COLOR 04");
	                cout << " Wrong Number Too low! " << endl; 
                }
              //break;           This break makes no sense, so I commented it out
                cout << "\n\n";
                cout << " Try Again Y/N? \n" << endl;
                char k;
                cin >> k;
        } while(k == 'y'); // Go over and over again, until the user inputs something that isn't equal to 'y'
}
Small notice: the above has some scope issues
Ok im still confused if you ran the program its self then maybe youd see my problem more clearly.
@Bazzy:
Ah, yes, it's getting late again, eh? :P
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
#include <iostream>

using namespace std;

int main()
{
        char k = 'y';
        do
        {
	        system ("COLOR 03");
	        system ("TITLE The Guessing Game! By: Jessie Keeton");
                int i = 46;
                int j = 0;
                int k = 0;
                cout << "**| What Is The Number Im Thinking Of?  |** " << endl;
                cin >> j;
                if (i == j)
                {
	                system ("COLOR 12");
        	        cout << " Correct! " << endl;
                }
                else if(j > i)
                {
	                cout << " Wrong ansewer Too high! " << endl;
	                system ("COLOR 04");
                }
                else
                {
        	        system ("COLOR 04");
	                cout << " Wrong Number Too low! " << endl; 
                }
              //break;           This break makes no sense, so I commented it out
                cout << "\n\n";
                cout << " Try Again Y/N? \n" << endl;
                cin >> k;
        } while(k == 'y'); // Go over and over again, until the user inputs something that isn't equal to 'y'
}


I'm heading off before I make more horrid coding mistakes. :P
Last edited on
Keeton, what is the problem? This works just fine. Do you mean that the screen doesn't clear between games?
It was the fact that the char was created inside of the loop and was checked outside. Some compilers might give an error to that.
Last edited on
HAHA well it kinda works I just now understand what do and when commands mean but now when i press Y it repeats this **| What Is The Number Im Thinking Of? |** Wrong number ! constantly in a loop......... do i need to put a pause inside the do?
BUMP! :D
Bump again...........
do i need to put a pause inside the do?


Whatever you want to repeat goes inside the do{} block.

If you want the program to pause every time it loops, then you should put the pause in the loop.



Although in the 24 hours you were waiting for a response from the forum, you could have just tried it and found out for yourself whether or not it worked.
Topic archived. No new replies allowed.