-3

Write a C++ program that simulates the casino game of craps. These are the rules of the game: • If a player throws a 7 or 11 (sum of two dice) on the first roll, the player wins the game. • If a player throws a 2, 3 or 12 (sum of two dice) on the first roll, the player loses the game. • If a player throws a 4, 5, 6, 8, 9 or 10 (sum of two dice) on the first roll, s(he) neither wins nor loses but creates a “point.” If this is the case, the player keeps rolling the dice until the point (4, 5, 6, 8, 9 or 10) is thrown again, and the player wins the game. However, if the player throws a 7 (sum of two dice) before the “point” is thrown, the player loses the game.

You will create a function called rollDice that will, when called, roll two dice and return a random number between 2 and 12. Each time rollDice is called, the program should output the result of the roll

The program will ask the player ” Another game? Y(es) or N(o)?” and terminate whenever any key other than Y or y is pressed.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
int dice1, dice2 = 0; 
int rollDice;
char repeat = 'y'; 

cout << "**********************************************************"<< endl;
cout << "********   Welcome to the Kyung Bae Choi Casino   ********"<< endl;
cout << "********* Step up to the table and place your bets! ******"<< endl;
cout << "**********************************************************"<< endl;

while (repeat == 'y' || repeat == 'Y') 
{                                                   
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1; 
    rollDice = dice1 + dice2;

    cout << "Your rolled " << rollDice;
    if (rollDice == 7 || rollDice == 11)
    {
        cout << ". Winner !" << endl ;
    } 

    else if (rollDice == 2 || rollDice == 3 || rollDice == 12)
    {
        cout  << ". You lose!" << endl; 
    }

    else if (rollDice == 4 || rollDice == 5 ||rollDice == 6 ||rollDice == 8 || rollDice == 9 || rollDice == 10)
      {     
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1; 
    int sum2 = dice1 + dice2;

     if( sum2 == rollDice )
            {
                cout << ". Winner !" << endl;
                break;
            } 
    else if( sum2 == 7 )
            {
                cout << ". You Lose!" << endl;
                break;
            }
  }
cout <<"Another game? Y(es) or N(o)"  << endl;
    cin >> repeat;

while (repeat == 'n' || repeat == 'N') 
{
cout <<  "Thank you for playing!"<< endl;
}
return 0;
}
}

This does output only 7. so, it keep saying Your rolled 7. Winner ! Another game? Y(es) or N(o). And, if i put y, the program ends. or if i put n, the program output the "thank you for playing!" endlessly.

what is problem??

How can I make it continually re-rolling the dice until the player rolls the winning "point" or rolls a seven.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Kyungbae Choi
  • 1
  • 1
  • 1
  • 3
  • http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand – Lundin Nov 17 '14 at 07:18
  • as you are using `while` loop that's why is you enter `n` or `N` condition will always true and it will will print message infinite time. Best thing is to use `if` condition. – Himanshu Nov 17 '14 at 07:20

3 Answers3

2

You're missing the "...." in the coutstatement. So, the compiler is fooled to belive that Thank, you,... are instructions [keywords/variables] which is actually wrong.

change

cout << Thank you for playing!<< endl;

to

cout <<"Thank you for playing!"<< endl;

Thumb Rule: Always try to look into the information provided by the compiler during error/warning meessage. Here, the line number 58. It is really helpful to pinpoint the erroneous instructuion.


EDIT:

Please do not edit the current qusetion and add a completely new query. Reatain the previous version and metion the EDIT. Otherwise, it makes the previous answers irrelevant.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You're missing quotes in cout << Thank you for playing!<< endl;.

cout << "Thank you for playing!" << endl;

Also, your logic for testing repeat == 'n' or 'N' is wrong:

  1. You should use an if statement, not a while loop.
  2. You should quit the program only if the user enters 'N' or 'n'.

To notice the second error, it is helpful to use a standard indentation style.

irrelephant
  • 4,091
  • 2
  • 25
  • 41
0

Use Double qoutes (") while printing a string..

cout <<"Thank you for playing!"<< endl;

And One more thing as suggested by @irrelephant in his answer

use

if(repeat == 'n' || repeat == 'N')                 //Use if condition
{
   cout <<  Thank you for playing!<< endl;
   return 0;                                      // exit only when user enter `n` or `N`
} 

If you use return 0; outside then no matter what user has entered Y or N, program will terminate.

Himanshu
  • 4,327
  • 16
  • 31
  • 39