Write a program that will allow the user to roll two dice

Write a program that will allow the user to roll two dice, elementary uses of:

printf( ) function

scanf ( ) function

while OR do --- while

if OR if --- else

rand()

If the sum of the roll of the dice is 7, the user will win $50.

· If the sum is anything else the user will lose $10.

· Give each user a starting bankroll of $100. Keep a running total of the amount won/lost and report it at the end of each roll.

· Allow the user to roll

· until he/she enters a 0 (no) OR

· until the user has a bankroll less than $0.

· When the user enters a 0 (or has a bankroll less than $0), put out an ending message
If the user has won money
“Thanks for playing. You have won X” ß
If the user has lost money (has a bankroll less than $100)
“Thanks for playing. You have lost X” ß

(X = 100 - the running total)

I need help writing this program. How do I incorporate the bankroll values. My first program looked like this and then I add a second easy beginning program. I just need help.

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
int main ()
{
int sum; /* initialize sum */
int bankRoll; /* money earned */

enum Status gameStatus; /* can contain CONTINUE, WON, LOST */

sum = rollDice(); /* first roll of the dice */

/* determine game status based on sum of dice */
switch( sum ){

/* win on first roll */
case 7:
gameStatus = WON;
break;

/* lose on first roll */
case 2:
case 5:
case 10:
gameStatus = LOST;
break;

/* remember money */
default:
gameStatus = CONTINUE;
bankRoll = sum;
printf( "Bank Roll is %d\n", bankRoll );
break; /*optional */
} /* end swicth */

/* while game not complete */
while ( gameStatus == CONTINUE ) {
sum = rollDice(); /* roll dice again */

/* determine game status */
if ( sum == bankRoll ) { /* win by making money */
gameStatus = WON; /* game over, player won */
} /* end if */
else {
if ( sum == 7 ) { /* lose by rolling 7 */
gameStatus = LOST; /* game over, player lost */
} /* end else */

/* display won or lost message */
if ( gameStatus == WON ) {
printf( "Thanks for playing. You have won\n" );
} /* end if */
else {
if ( sum == LOST ) {
printf( "Thanks for playing. You have lost\n" );
} /* end if */
system("pause");
return 0; /* indicates progam ended successfully */
} /* end main */

/* roll dice, calculate sum and display results */
int rollDice()
{
int die1; /* first die */
int die2; /* second die */
int workSum; /* sum of dice */

die1 = 1 + ( rand() % 6 ); /* pick random die1 value */
die2 = 1 + ( rand() % 6 ); /* pick random die2 value */
workSum = die1 + die2; /* sum die1 and die2 */

/* display results of this roll */
printf( "Player rolled %d + %d = %d\n", die1, die2, workSum );
return workSum; /* return sum of dice */
} /* end function rollDice */ 


My second program to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"


int main()
{
	int die1; /* first die */
	int die2; /* second die */
	int sum; /* sum of dice */

	die1 = 1 + ( rand() % 6 ); 
	die2 = 1 + ( rand() % 6 );
	sum = die1 + die2;

	return 0;
}
If I understood it correctly...

1 - bankroll should be initialized to 100 - why is it receiving the sum of your dice?;
2 - every time you win, bankroll +50;
3 - every time you lose, bankroll -10.

2 and 3 can be done in your "cases". Of course you'll have to check bankroll after every play so it doesn't go below 0 (gameStatus = ? ).

I hope I was able to help, but not too much. ;)
This isn't exactly what you're looking for, but I think it's similar. Your program is obviously a craps game, although it's modified...If you don't roll a certain number to win or certain numbers to lose the first time, then you usually try to roll until either you get to 7, when you would lose, or the number that you originally rolled, in which case you would win.

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
#include <iostream>
#include <ctime>
#include <stdlib.h>

using namespace std;

int rollDice();

int main()
{
    int Account = 100;

    enum Status{WON, LOST, CONTINUE};
    int myPoint = 0;

    Status gameStatus;
    srand(time(0));

    while (Account > 0 && Account <= 500){

    int sumOfDice = rollDice();

    switch(sumOfDice){

case 7:
case 11:
    Account += 10;
    gameStatus = WON;
    break;

case 3:
case 4:
case 12:
    Account -= 10;
    gameStatus = LOST;
    break;

default:
    gameStatus = CONTINUE;
    myPoint = sumOfDice;
    cout << "Point is " << myPoint << endl;
    break;
    }

    while(gameStatus == CONTINUE){

        sumOfDice = rollDice();

        if (sumOfDice == myPoint){
            gameStatus = WON;
            Account += 10;
        }

        if (sumOfDice == 7){
            gameStatus = LOST;
            Account -= 10;
        }
}

  if (gameStatus == WON)
        cout << "\nPlayer wins! Bank Roll is now $" << Account << "." << endl;
    if (gameStatus == LOST)
        cout << "\nPlayer loses! Bank Roll is now $" << Account << "." << endl;

  }

cout << "\nBank Roll is now zero - You're done! \n";

}

int rollDice(){

int dice1 = 1 + rand()%6;
int dice2 = 1 + rand()%6;

int total = dice1 + dice2;

cout << "Player rolls " << dice1 << " and " << dice2 << " for a total of " << total << endl;

return total;

}
Last edited on
Topic archived. No new replies allowed.