Thread: exit(0); Help please

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    22

    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.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    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

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    17
    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;

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    *slaps self*

    This time I tested my answer.
    Code:
    start:
    	while (cin.get() == NULL)
    		goto start;
    	return 0;

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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;
    Woop?

  7. #7
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    ahhhhhh, I'll shut up for the rest of today. I don't know what I've done =/
    At least it worked

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Thanks everyone. Now I can do my English talk and have a perfectly working exapmle (even if it's not random or fun...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exit(0) and Infinite Loop
    By kpreston in forum C Programming
    Replies: 27
    Last Post: 10-27-2008, 09:32 PM
  2. Before your program does exit(0);
    By ycode in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 11:47 PM
  3. Destructors & exit(0)
    By GrNxxDaY in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2002, 07:07 PM
  4. exit(); and exit(0);
    By iain in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2002, 08:37 PM
  5. exit(0)
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2001, 06:01 AM