This way it should work. I have changed a few things ( like recoursive calls ).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* contains prototype for function time */
#define beginbalance 1000
/* enumeration constants represent game status */
enum Status { CONTINUE, WON, LOST };
int rollDice(void); /* rollDice function prototype */
/* playGame doesn't neet to return anything */
void playGame(int * balance ); /* playGame function prototype */
/* getWager needs to know the current balance to check for valid amount
balance will not be changed so pass it by value */
int getWager( int balance );
/* adjustBalance changes the balance so we pass it by reference
need to pass the game result to know wheter to add or subtract the wager
wager and result are not changed we can pass it by value */
void adjustBalance(int * balance, int wager, int result );
/* getYesOrNo will display the current balance so we pass it by value
return 1 if the player wants to play another time */
int getYesOrNo( int balance );
int main() {
/* initialize balance only once */
int balance = beginbalance;
/* randomize random number generator using current time
should be done only once in program */
srand( time( NULL ) );
/* main game loop: ends if player stops or runs out of money */
do {
playGame( & balance );
} while ( balance > 0 && getYesOrNo( balance ) == 1 );
if ( balance == 0 )
printf("Sorry, You ran out of money\n");
return 0;
}
/* PRE: none
POST: none */
void playGame ( int * balance ) {
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int result = 0; /* integer defining whether or not last game was WON or LOST */
printf("Balance = $%d.00\n", * balance);
int wager = getWager( *balance );
enum Status gameStatus; /* can contain CONTINUE, WON, or LOST */
sum = rollDice(); /* first roll of the dice */
/* determine game status based on sum of dice */
switch( sum ) {
/* win on first roll */
case 7:
case 11:
gameStatus = WON;
break;
/* lose on first roll */
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
/* remember point */
default:
gameStatus = CONTINUE;
myPoint = sum;
printf( "Point is %d\n", myPoint );
break; /* optional */
} /* end switch */
/* while game not complete */
while ( gameStatus == CONTINUE ) {
sum = rollDice(); /* roll dice again */
/* determine game status */
if ( sum == myPoint ) { /* win by making point */
gameStatus = WON; /* game over, player won */
} /* end if */
else {
if ( sum == 7 ) { /* lose by rolling 7 */
gameStatus = LOST; /* game over, player lost */
} /* end if */
} /* end else */
} /* end while */
/* display won or lost message */
if ( gameStatus == WON ) { /* did player win? */
printf( "Player wins\n" );
result = 1;
} /* end if */
else { /* player lost */
printf( "Player loses\n" );
result = -1;
} /* end else */
adjustBalance( balance, wager, result );
}
/* roll dice, calculate sum and display results */
int rollDice(void)
{
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 rollRice */
/* PRE: player inputs a dollar amount to wager on next game
POST: function checks wager, if wager is invalid, prompt user to
enter new wager.
when valid wager is entered, returns to the calling function */
int getWager( int balance ) {
int amount = 0;
int amount_ok = 0;
/* loop until a valid amount is entered */
while ( !amount_ok ) {
printf("Enter wager: ");
scanf("%d", &amount);
if ( amount <= balance )
amount_ok = 1;
else
printf("Your wager must not exceed your current balance.\n");
}
return amount;
}
/* PRE: checks whether or not the last game was WON or LOST
POST: either adds or subtracts the wager from the player's current
balance */
void adjustBalance(int * balance, int wager, int result ) {
*balance = ( *balance) + wager * result;
printf("Balance = $%d.00\n",*balance);
}
/* PRE: asks if the user desires to play another game of craps
POST: function checks the response to make sure it is either 'y' or 'n'.
the function will repeatedly ask until one of these conditions is satisfied
and upon a valid answer, return 0 or 1 to the calling function */
int getYesOrNo(int balance) {
char ch;
int ret = -1;
printf("Do you want to play another game? (y or n): ");
while( ret == -1 ) {
ch = getchar();
if (ch == 'y') {
ret = 1;
}
else if (ch == 'n'){
printf("\n\nYour final balance is $%d.00\n",balance);
ret = 0;
}
/* ignore newlines */
else if (ch != '\n' ) {
printf("You must answer y or n.");
}
}
// clear remaining characters in buffer
while ((ch = getchar() ) != '\n');
return ret;
}
Kurt