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.
#include <iostream>;
usingnamespace 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;
}
elseif(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");
}
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.
#include <iostream>;
usingnamespace 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;
}
elseif(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'
}
#include <iostream>
usingnamespace 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;
}
elseif(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
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?
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.