S
Shock
Hello everyone. I have never posted here so please forgive me if I
step on toes. I am learning c++ on my own and I have a fairly simple
question. Exercise 3.54 is very easy except one thing. It wants you
to use a while loop where I deem it easy and logical to use an if/else
statement. Can anyone give me an idea of how to use the while to test
for what is asked and to do something different if the situation in
the while is false. I would normally do it like this
if ( Wager <= BankBalance ) {
...PERFORM STUFF...}
else (Wager > BankBalance ) {
...PERFORM STUFF...}
I attached the question below as well as my source to this point.
Keep it mind it works except I am having trouble getting things to
work in the right order. Thanks in advance.
3.54 Modify the craps program of Fig. 3.10 to allow wagering.
Package as a function the portion of the program that runs one game of
craps. Initialize variable bankBalance to 1000 dollars. Prompt the
player to enter a wager. Use a WHILE loop to check that wager is less
than or equal to bankBalance and, if not, prompt the user to reenter
wager until a valid wager is entered. After a correct wager is
entered, run one game of craps. If the player wins, increase
bankBalance by wager, print the new bankBalance, check on whether
bankBalance has become zero and, if so, print the message "Sorry. You
busted!" As the game progresses, print various messages to create
some "chatter" such as "Oh, you're going for broke, huh?", "Aw cmon,
take a chance!" or "You're up big. Now's the time to cash in your
chips!".
My Source <-->
// Question 3.54 - Chapter 3
// Author: Shock56
// Description: Craps Simulation
#include <iostream>
#include <cstdlib> // contains function prototypes for functions
srand and rand
#include <ctime> // contains prototype for function time
using std::cout;
using std::endl;
using std::cin;
// enumeration constants represent game status
enum Status { CONTINUE, WON, LOST };
int rollDice( void ); // function prototype
void playGame ( Status & gameStatus );
void displayWonOrLost ( Status, int &, int & );
int main()
{
int BankBalance = 1000;
int Wager;
char Response;
Status gameStatus; // can contain CONTINUE, WON or LOST
cout << "Please enter a wager (You start with 1000 USD!)" << endl;
cin >> Wager;
while ( Wager <= BankBalance ) {
cout << "Continue with current wager (Y), enter a new wager (E) or
end game (N)?" << endl;
cin >> Response;
switch ( Response ) {
case 'Y':
case 'y':
playGame( gameStatus );
displayWonOrLost ( gameStatus, BankBalance, Wager );
break;
case 'N':
case 'n':
cout << "Your final balance is " << BankBalance << endl;
cout << "Thank you, come again!" << endl;
return 0;
break;
case 'E':
case 'e':
cout << "Please enter the new wager!" << endl;
cin >> Wager;
break;
default:
cout << "Incorrect Response. Please try again!" << endl;
break;
} // end switch
} // end while
/*if ( Wager > BankBalance ) {
cout << "Re-enter wager!" << endl;
cin >> Wager;
} */
if ( BankBalance == 0 )
cout << "Sorry, you busted!" << endl;
return 0; // indicates successful termination
} // end main
void displayWonOrLost ( Status gameStatus, int & passedBalance, int &
passedWager )
{
// display won or lost message
if ( gameStatus == WON ) {
passedBalance += passedWager;
cout << "Player wins, new balance is " << passedBalance << endl;
cout << "You are awesome! Keep Going!" << endl;
}
else {
passedBalance -= passedWager;
cout << "Player loses, new balance is " << passedBalance << endl;
cout << "You suck! Get out while you can!" << endl;
} // End display won or lost message
}
void playGame (Status & gameStatus)
{
int sum;
int myPoint;
// randomize random number generator using current time
srand( time( 0 ) );
sum = rollDice(); // first roll of the dice
// determine game status and point 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;
cout << "Point is " << myPoint << endl;
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;
else
if ( sum == 7 ) // lose by rolling 7
gameStatus = LOST;
} // end while
} // end playGame
// roll dice, calculate sum and display results
int rollDice( void )
{
int die1;
int die2;
int workSum;
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
cout << "Player rolled " << die1 << " + " << die2
<< " = " << workSum << endl;
return workSum; // return sum of dice
} // end function rollDice
Thanks again!
Shock56
step on toes. I am learning c++ on my own and I have a fairly simple
question. Exercise 3.54 is very easy except one thing. It wants you
to use a while loop where I deem it easy and logical to use an if/else
statement. Can anyone give me an idea of how to use the while to test
for what is asked and to do something different if the situation in
the while is false. I would normally do it like this
if ( Wager <= BankBalance ) {
...PERFORM STUFF...}
else (Wager > BankBalance ) {
...PERFORM STUFF...}
I attached the question below as well as my source to this point.
Keep it mind it works except I am having trouble getting things to
work in the right order. Thanks in advance.
3.54 Modify the craps program of Fig. 3.10 to allow wagering.
Package as a function the portion of the program that runs one game of
craps. Initialize variable bankBalance to 1000 dollars. Prompt the
player to enter a wager. Use a WHILE loop to check that wager is less
than or equal to bankBalance and, if not, prompt the user to reenter
wager until a valid wager is entered. After a correct wager is
entered, run one game of craps. If the player wins, increase
bankBalance by wager, print the new bankBalance, check on whether
bankBalance has become zero and, if so, print the message "Sorry. You
busted!" As the game progresses, print various messages to create
some "chatter" such as "Oh, you're going for broke, huh?", "Aw cmon,
take a chance!" or "You're up big. Now's the time to cash in your
chips!".
My Source <-->
// Question 3.54 - Chapter 3
// Author: Shock56
// Description: Craps Simulation
#include <iostream>
#include <cstdlib> // contains function prototypes for functions
srand and rand
#include <ctime> // contains prototype for function time
using std::cout;
using std::endl;
using std::cin;
// enumeration constants represent game status
enum Status { CONTINUE, WON, LOST };
int rollDice( void ); // function prototype
void playGame ( Status & gameStatus );
void displayWonOrLost ( Status, int &, int & );
int main()
{
int BankBalance = 1000;
int Wager;
char Response;
Status gameStatus; // can contain CONTINUE, WON or LOST
cout << "Please enter a wager (You start with 1000 USD!)" << endl;
cin >> Wager;
while ( Wager <= BankBalance ) {
cout << "Continue with current wager (Y), enter a new wager (E) or
end game (N)?" << endl;
cin >> Response;
switch ( Response ) {
case 'Y':
case 'y':
playGame( gameStatus );
displayWonOrLost ( gameStatus, BankBalance, Wager );
break;
case 'N':
case 'n':
cout << "Your final balance is " << BankBalance << endl;
cout << "Thank you, come again!" << endl;
return 0;
break;
case 'E':
case 'e':
cout << "Please enter the new wager!" << endl;
cin >> Wager;
break;
default:
cout << "Incorrect Response. Please try again!" << endl;
break;
} // end switch
} // end while
/*if ( Wager > BankBalance ) {
cout << "Re-enter wager!" << endl;
cin >> Wager;
} */
if ( BankBalance == 0 )
cout << "Sorry, you busted!" << endl;
return 0; // indicates successful termination
} // end main
void displayWonOrLost ( Status gameStatus, int & passedBalance, int &
passedWager )
{
// display won or lost message
if ( gameStatus == WON ) {
passedBalance += passedWager;
cout << "Player wins, new balance is " << passedBalance << endl;
cout << "You are awesome! Keep Going!" << endl;
}
else {
passedBalance -= passedWager;
cout << "Player loses, new balance is " << passedBalance << endl;
cout << "You suck! Get out while you can!" << endl;
} // End display won or lost message
}
void playGame (Status & gameStatus)
{
int sum;
int myPoint;
// randomize random number generator using current time
srand( time( 0 ) );
sum = rollDice(); // first roll of the dice
// determine game status and point 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;
cout << "Point is " << myPoint << endl;
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;
else
if ( sum == 7 ) // lose by rolling 7
gameStatus = LOST;
} // end while
} // end playGame
// roll dice, calculate sum and display results
int rollDice( void )
{
int die1;
int die2;
int workSum;
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
cout << "Player rolled " << die1 << " + " << die2
<< " = " << workSum << endl;
return workSum; // return sum of dice
} // end function rollDice
Thanks again!
Shock56