Craps game help

Hello All. Im having trouble with the "point" part of this program. If the user rolls a result that's other than 7,11,2,3,12, then the program should store that result as a point, and only roll the dice once more and compare it to the Point. but instead, if the user rolls a point, the die is being rolled more than once. not sure why..craps rules below. thanks in advance.

--------------------------------------------

Create a program to allow a user to play a modified “craps” games. The rules will be as follows:

1) User starts with $50.00 as their total.
2) Ask the user for their bet (maximum bet is their current total).
3) Throw a pair of dice.

a) If the total value is 7 or 11, the user is an instant winner. The user has the amount bet added back into their total. Ask the user if they wish to play again and if so they start at step two above.
b) If the total value is 2, 3, or 12, the user is an instant loser. The user has the amount bet deducted from their total. Ask the user if they wish to play again and if so they start at step two above.
c) If the total is anything else, remember this total as the “point” and roll again.

i) If the new total is equal to the “point”, the user wins and the process is the same as winning in (a) above
ii) If the new total is a 7, the user loses. And the process is the same as losing in (b) above.
iii) If the new total is anything else the user must roll again and again try to match the “point” of the first roll.

------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "stdafx.h"
#include <iostream>
#include <random>
#include <time.h>
#include <iomanip>

using namespace std;

//Returns the value of two rolled dice
int rollDice()
{
	//Returns a value between 2 and 12
	return ((rand() % 6) + 1) + ((rand() % 6) + 1);
}

void playAgain(bool& answer)
{
	char ch = 'a';

	while (toupper(ch) != 'Y' && toupper(ch) != 'N')
	{
		cout << "Would you like to play again? (Y) or (N) \n\t>>";
		cin >> ch;
	}

	if (toupper(ch) == 'Y') { answer = false; }
	if (toupper(ch) == 'N') { answer = true; }
}

int main()
{
	srand((unsigned int)time(NULL)); //Seed the random number generator

									 //The starting money and the bet
	float money = 50.00;
	float bet = 0.0;
	bool donePlaying = false;
	int roll1 = 0, roll2 = 0;

	//Start the game loop
	while (!donePlaying)
	{
		while (bet <= 0.0 || bet > money)
		{
			cout << "Please enter a bet between $0.01 and $" << fixed << setprecision(2) << money << "\n\t>>";
			cin >> bet;
		}
		roll1 = rollDice();
		cout << "You rolled a " << roll1 << "!\n";

		if (roll1 == 7 || roll1 == 11)
		{
			money += bet;
			cout << "You win! Your new balance is: $" << fixed << setprecision(2) << money << endl;
		}
		else if (roll1 == 2 || roll1 == 3 || roll1 == 12)
		{
			money -= bet;
			cout << "You lose! Your new balance is: $" << fixed << setprecision(2) << money << endl;
		}
		else
		{
			//if next roll is 7 its an loose , or we roll once more
			while (roll2 != 7 && roll2 != roll1)
			{
				roll2 = rollDice();
				cout << "Your next roll was a " << roll2 << "!\n";
			}
			if (roll2 == roll1)
			{
				money += bet;
				cout << "You win! Your new balance is: $" << fixed << setprecision(2) << money << endl;
			}
			else if (roll2 == 7)
			{
				money -= bet;
				cout << "You lose! Your new balance is: $" << fixed << setprecision(2) << money << endl;
			}
		}

		bet = 0.0;
		//If thety still have money, they can pla\y again
		if (money > 0)
			playAgain(donePlaying);
		else
			donePlaying = true;

	}

	cout << "\nThanks for playing! Your final balance was : $" << fixed << setprecision(2) << money << endl;

	//Pause the program
	cin.sync();
	cin.get();
	return 0;
}  
Last edited on
You need a value for “roll2“ before this line:
while (roll2 != 7 && roll2 != roll1)

Hints:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Create a program to allow a user to play a modified “craps” games. The rules 
// will be as follows:
// 1) User starts with $50.00 as their total.
// 2) Ask the user for their bet (maximum bet is their current total).
// 3) Throw a pair of dice.
// a) If the total value is 7 or 11, the user is an instant winner. The user 
//    has the amount bet added back into their total. Ask the user if they wish 
//    to play again and if so they start at step two above.
// b) If the total value is 2, 3, or 12, the user is an instant loser. The user 
//    has the amount bet deducted from their total. Ask the user if they wish 
//    to play again and if so they start at step two above.
// c) If the total is anything else, remember this total as the “point” and 
//    roll again.
//      i) If the new total is equal to the “point”, the user wins and the 
//         process is the same as winning in (a) above
//     ii) If the new total is a 7, the user loses. And the process is the same 
//         as losing in (b) above.
//    iii) If the new total is anything else the user must roll again and again 
//         try to match the “point” of the first roll.
#include <iostream>
// --------------------------------------------------------------
//#include <random>        <-- C++11 new awesome random features
#include <cstdlib>      // <-- C legacy "rand()"
// --------------------------------------------------------------
//#include <time.h>        <-- C header
#include <ctime>        // <-- C++ header
// --------------------------------------------------------------
#include <iomanip>

using namespace std;    // For experts only. Better avoided.

//Returns the value of two rolled dice
int rollDice()
{
    //Returns a value between 2 and 12
    return ((rand() % 6) + 1) + ((rand() % 6) + 1);
}

void playAgain(bool& answer) // wouldn't it be simpler to just return a bool?
{
    char ch = 'a';

    while (toupper(ch) != 'Y' && toupper(ch) != 'N')
    {
        cout << "Would you like to play again? (Y) or (N) \n\t>>";
        cin >> ch;
    }

    if (toupper(ch) == 'Y') { answer = false; }
    if (toupper(ch) == 'N') { answer = true; }
}

int main()
{
    srand((unsigned int)time(NULL)); //Seed the random number generator

    //The starting money and the bet
    float money = 50.00;            // prefer "double" to "float"
    bool donePlaying = false;

    //Start the game loop
    while (!donePlaying)
    {
        float bet = 0.0;            // prefer "double" to "float"
        while (bet < 0.1 || bet > money)
        {
            cout << "Please enter a bet between $0.01 and $" << fixed
                 << setprecision(2) << money << "\n\t>>";
            cin >> bet;
        }
        int roll1 = rollDice();
        cout << "You rolled a " << roll1 << "!\n";

        if (roll1 == 7 || roll1 == 11)
        {
            money += bet;
            cout << "You win! Your new balance is: $" << fixed 
                 << setprecision(2) << money << endl;   // use '\n' instead of std::endl
        }
        else if (roll1 == 2 || roll1 == 3 || roll1 == 12)
        {
            money -= bet;
            cout << "You lose! Your new balance is: $" << fixed 
                 << setprecision(2) << money << endl;   // use '\n' instead of std::endl
        }
        else
        {
            
            for(int roll2 = rollDice(); /**/ ; roll2 = rollDice()) // forever
            {
                cout << "Your next roll was a " << roll2 << "!\n";
                // if roll2 is 7 it's a loose, if matches roll1 is a win,
                // otherwise we roll again
                if (roll2 == 7)
                {
                    money -= bet;
                    cout << "You lose! Your new balance is: $" << fixed
                          << setprecision(2) << money << endl;  // use '\n' instead of std::endl
                    break;
                }
                else if (roll2 == roll1)
                {
                    money += bet;
                    cout << "You win! Your new balance is: $" << fixed 
                         << setprecision(2) << money << endl;   // use '\n' instead of std::endl
                    break; // exit for
                }
            }
        }
        bet = 0.0;
        //If thety still have money, they can pla\y again
        if (money > 0) { playAgain(donePlaying); }
        else           { donePlaying = true; }
    }

    cout << "\nThanks for playing! Your final balance was : $" << fixed 
         << setprecision(2) << money << endl;   // use '\n' instead of std::endl

    //Pause the program
    cin.get();
    return 0;
}

To “pause” a program please read here:
http://www.cplusplus.com/forum/beginner/1988/
Anyway, adding
std::cin.ignore(1000, '\n');
before your cin.get(); should work (I haven't tested it).
Topic archived. No new replies allowed.