Thread: ending problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    ending problem

    okay here is the problem. You see I am writing a dice rolling game. Kinda like craps. I got the dice rolling part all worked out. But the problem is, When the player wins or loses, it closes. Soa ny help would be great.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <time.h>
    
    int rollDice( void );       // function prototype
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     enum Status { CONTINUE, WON, LOST };
     int sum, myPoint;
     Status gameStatus;
     
     srand( time( NULL ) );
     sum = rollDice();
     
     switch ( sum ) {
            case 7:
            case 11:
                 gameStatus = WON;
                 break;
            case 2:
            case 3:
            case 12:
                 gameStatus = LOST;
                 break;
            default:
                 gameStatus = CONTINUE;
                 myPoint = sum;
                 cout << "point is "<< myPoint;
                 break;
    }
    
    while ( gameStatus == CONTINUE ) { //keep rolling
          sum = rollDice();
          
          if ( sum == myPoint )
             gameStatus = WON;
          else
              if ( sum == 7 )
                 gameStatus = LOST;
    }
    
    if ( gameStatus == WON )
       cout << "player wins";
       else
       cout << "player loses";
       
       return 0;
       
    }
              
    int rollDice( void )
    {
        int die1, die2, workSum;
        
        die1 = 1 + rand() %6;
        die2 = 1 + rand() %6;
        workSum = die1 + die2;
        cout << "player rolled " << die1 << " + " << die2
             << " = \n" << workSum;
        
        system("PAUSE"); 
        return workSum;
        return EXIT_SUCCESS;
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    You have a "return 0;' after the win or lose message. What are you expecting to happen?

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    hmm i didnt even see that.. thanks for pointing that out...
    okay, i just took out the return 0; but, the thing is. It is still doing it. I'm now a bit lost..
    Last edited by Gardul; 10-21-2005 at 11:04 AM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The way I see it coded, it looks like it's supposed to close when they win or lose. If you want it to ask for a new game or continue then you have to the gamestatus == WON if statement into the loop and change the condition of the loop.
    Sent from my iPad®

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    what should i do? I'm semi new and dont have much experence with loops. any ideas or examples would help. thanks.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int rollDice( void )
    {
        int die1, die2, workSum;
        
        die1 = 1 + rand() %6;
        die2 = 1 + rand() %6;
        workSum = die1 + die2;
        cout << "player rolled " << die1 << " + " << die2
             << " = \n" << workSum;
        
        system("PAUSE"); 
        return workSum;
        return EXIT_SUCCESS;
    }
    That code will never be executed. Get rid of it.

    Wait, did you just change that return 0 to return EXIT_SUCCESS?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    I think when I started DEV it was on there. since i dont remember typing it.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Here, I added a newgame loop for you. Study what I did so you can understand it.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <time.h>
    
    int rollDice( void );       // function prototype
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       enum Status { CONTINUE, WON, LOST };
       int sum, myPoint;
       char newGame;
       Status gameStatus;
     
       srand( time( NULL ) );
       
       do {
          sum = rollDice();
          switch ( sum ) {
            case 7:
            case 11:
                 gameStatus = WON;
                 break;
            case 2:
            case 3:
            case 12:
                 gameStatus = LOST;
                 break;
            default:
                 gameStatus = CONTINUE;
                 myPoint = sum;
                 cout << "point is " << myPoint << endl;
                 break;
          }
            
           while ( gameStatus == CONTINUE ) { //keep rolling
            sum = rollDice();     
            if ( sum == myPoint ) 
               gameStatus = WON;
            else if ( sum == 7 ) 
               gameStatus = LOST;
            }
            
           if ( gameStatus == WON ) 
            cout << "Player wins!" << endl;  
           else if ( gameStatus == LOST )
            cout << "Player loses..." << endl;
            
           cout << "New Game? (y/n)" << endl; 
           cin >> newGame; 
          } while (newGame == 'y' || newGame == 'Y');
                
       return 0;
       
    }
              
    int rollDice( void )
    {
        int die1, die2, workSum;
        
        die1 = 1 + rand() %6;
        die2 = 1 + rand() %6;
        workSum = die1 + die2;
        cout << "player rolled " << die1 << " + " << die2
             << " = " << workSum << endl;
        
        system("PAUSE"); 
        return workSum;
        return 0;
    }
    Sent from my iPad®

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
        return workSum;
        return 0;
    Two return statements! Again!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    OMG... i cant belive i didnt figure that.. Thank you very much.. you have helped me understand this just a bit more. i hope you all have a good day for helping me

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    while (newGame == 'y' || newGame == 'Y');
    In cases like this, tolower() is your friend:
    Code:
    while(tolower(newGame) == 'y');
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by dwks
    Code:
    while (newGame == 'y' || newGame == 'Y');
    In cases like this, tolower() is your friend:
    Code:
    while(tolower(newGame) == 'y');
    tolower(), I'll have to remember that one. Thanks.
    Sent from my iPad®

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by SlyMaelstrom
    tolower(), I'll have to remember that one. Thanks.
    as will I

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem ending application
    By rbrodbeck in forum C Programming
    Replies: 6
    Last Post: 07-17-2008, 03:44 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM