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;
}
|