-
exit(0); Help please
Ok, first off, here's my code for a non-random rock, paper scissors game.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int x;
int y;
int z;
cout<<"Ok! It's time time play! It's 1 to play rock, 2 to play scissors and 3 to play paper! 1, 2, 3 GO!\n";
cin>> x;
if ( x == 2 ) {
cout<<"No! You won the first round! How could you, now it's time for you to die!\n";
cin.get();
}
else if ( x == 3 ) {
cout<<"A draw? Pah! We must pick once again!\n";
cin>> x;
if ( x == 2 ) {
cout<<"You.. You won! You can't! but I'll win the 2nd round!\n";
cin.get();
}
else {
cout<<"I won loser! Next round!\n";
cin.get();
}
}
else {
cout<<"Yes! I win, loser. Na na na na na! Better luck next time, loser!\n";
cin.get();
}
//First round complete! Now to do the second one!
cout<<"Now, onto the second round! Pick once again please!\n";
cin>> y;
if ( x == 2 && y == 3 ) {
cout<<"No! You've won the whole thing! How dare you!\n";
exit(0);
}
else if ( x == 1 && y == 2 ) {
cout<<"YES! Losers! I've won everything!\n";
exit(0);
}
else if ( y == 3 ) {
cout<<"No! You won the first round! How could you, now it's time for you to die!\n";
cin.get();
}
else if ( y == 1 ) {
cout<<"A draw? Pah! We must pick once again!\n";
cin>> x;
if ( y == 3 ) {
cout<<"You.. You won! You can't! but I'll win the 3rd round!\n";
cin.get();
}
else {
cout<<"I won loser! Next round!\n";
cin.get();
}
}
else {
cout<<"Yes! I win, loser. Na na na na na! Better luck next time, loser!\n";
cin.get();
}
//Second round done! Now to do the third one!
cout<<"Now, the final round, exciting eh? Whoever wins this wins everything!\n";
cin.get();
if ( z == 2 ) {
cout<<"No! You've won everything!\n";
cin.get();
}
else if ( z == 3 ) {
cout<<"A draw? Pah! We must pick once again!\n";
cin>> x;
if ( z == 2 ) {
cout<<"You.. You won! You can't! NO!\n";
cin.get();
}
else {
cout<<"Yes! I've won everything! Now it's time!\n";
cin.get();
}
}
else {
cout<<"Yes! I've won everything! Now it's time!\n";
cin.get();
}
}
The thing about it is that I want to exit if you win in the first 2 rounds but if you get them riht it instantly cuts off and I want the user to press enter before it cuts off. I've underline the place wher I'm confused at. I've been trying different things for about quarter of an hour so please tell me how, thanks.
-
Replace exit(0) with
Code:
cin >> y;
return 0;
That will wait for any key to be pressed, if it must be enter then you then you could make a while loop.
-
Just one thing, when I press enter after that it doesn't do anything but take a new line and you have to enter a number then press enter to exit it. Thanks MadCow257
-
here's something not exactly relevant, but maybe it's helpful. ;)
Code:
int r;
fprintf(stdout, "program finished. press [spacebar] to quit\n");
while((r = toupper(r = getch())) != ' ');
return 0;
-
*slaps self*
This time I tested my answer.
Code:
start:
while (cin.get() == NULL)
goto start;
return 0;
-
Omg don't use goto for such a simple problem. Madcow what are you thinking?
try this your cin>>'s are leaving a '\n' in the buffer that your cin.get() is picking up.
Code:
//put this right before the exit point
cin.ignore(80,'\n');
cin.get()
return 0;
-
ahhhhhh, I'll shut up for the rest of today. I don't know what I've done =/
At least it worked :)
-
Thanks everyone. Now I can do my English talk and have a perfectly working exapmle (even if it's not random or fun...)