Thread: Help with Craps game

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    Help with Craps game

    Can anyone help me out on this thing. This is my first assignment and can't figure it out. But I can't get out of the loop after I roll the point value or roll a 7 to lose after the first roll. The game will just keep going. Also I need to a way to error trap at the end when the game asks if you want to continue. I have to loop that to where if the user enters something other than Y or N then it will repeat. But if its a N then you get the good bye message and exit. If the user enters Y, then the game starts over. Thanks for the help!
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    void main()
    {	
    	srand((unsigned)time(NULL));
    
    		int firstRoll, nextRoll, die1, die2;
    		char playAgain;
    			
    	cout << "Welcome to Craps!" << endl << endl;
    
    	do
    	{
    		cout << "The game is about to begin..." << endl;
    
    			system("pause");
    		cout << endl;
    
    		die1 = (rand() % 6 + 1);
    		die2 = (rand() % 6 + 1);
    		firstRoll = die1 + die2;
    
    		cout << "You rolled a " << die1 << " and a " << die2 << " for a total of " << firstRoll << "." << endl;
    
    		if (firstRoll == 7 || firstRoll == 11)
    		{
    		cout << "Congratulations, you win!" << endl;
    		}
    
    		else if ((firstRoll == 2) || (firstRoll == 3) || (firstRoll == 12))
    		{
    			 cout << "Sorry, you lose" << endl;
    		}
    
    		else
    		{
    			do 
    			{
    				cout << "Your point value is: " << firstRoll << endl;
    				system("pause");
    				cout << endl;
    
    				die1 = (rand() % 6 + 1);
    				die2 = (rand() % 6 + 1);
    				nextRoll = die1 + die2;
    
    				cout << "You rolled a " << die1 << " and a " << die2 << " for a total of " << nextRoll << "." << endl;
    
    				if (nextRoll == firstRoll)
    				{
    				cout << "Congratulations, you rolled your point value. YOU WIN!" << endl << endl;
    				}
    			
    				else if (nextRoll == 7)
    				{
    				cout << "Sorry, you lost." << endl;
    				}
    
    				else
    				{
    				cout << "Please roll again." << endl;
    				system("pause");
    				cout << endl;
    				}
    			} while ((nextRoll != 7) || (nextRoll != firstRoll));
    			}
    				
    				
    					cout << "Would you like to play again (Y or N)? " << endl;
    					cin >> playAgain;
    
    					if ((playAgain == 'n') || (playAgain == 'N'))
    					{
    						cout << "Thanks for playing!" << endl;
    						system("pause");
    					}
    
    					else if ((playAgain == 'y') || (playAgain == 'Y'))
    					{
    					}
    
    					else 
    					{
    						cout << "Please enter either Y or N!" << endl;
    					}
    					//}while;	
    	}while ((playAgain == 'y') || (playAgain == 'Y'));
    	}// end of main
    Last edited by Salem; 09-14-2004 at 03:16 PM. Reason: CODE TAGS PEOPLE - READ THE INTRO MESSAGES

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
     } while ((nextRoll != 7) || (nextRoll != firstRoll));
    should be
    Code:
     } while ((nextRoll != 7) && (nextRoll != firstRoll));
    You want to continue the loop only if the roll is not seven AND the roll is not the same as the previous roll.

    Also, did you see the thread that said "<< !! Posting Code? Read this First !! >>" ? You posted code, so you should have probably read that first. It says use code tags... it would have been easier to see the error with them. You can edit your post and add the code tags.


    P.S. Congratulations on using the standard library headers... many new posters use the old ones. However, what's with void main()? Even though some compilers accept it, that has never been valid in C++. It should be int main(). If your instructor says to use void main, point them to this link (http://www.research.att.com/~bs/bs_faq2.html#void-main) and ask why they disagree with the creator of the language.

    And it looks like Salem's fixed the Code tags for you.
    Last edited by jlou; 09-14-2004 at 03:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 11:03 AM
  2. Craps Game
    By TWIXMIX in forum Game Programming
    Replies: 5
    Last Post: 06-12-2004, 07:47 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. help with a craps game!
    By Jessie in forum C Programming
    Replies: 10
    Last Post: 10-16-2002, 09:19 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM