Deitel 3.54

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
 
K

Kevin Goodsell

Shock said:
Hello everyone. I have never posted here so please forgive me if I
step on toes.

Hi. The best way to avoid this is to read the Welcome Message:

http://www.slack.net/~shiva/welcome.txt

2 things about your post that I would have preferred be different: 1)
less code, and 2) the question stated as a general C++ question. We
generally ask for the smallest complete program that demonstrates the
problem. That same general concept (keep it simple and to the point) can
be applied to all parts of the ideal post here.
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.

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.

The reason they want a loop is so that you can request valid input, test
it, and if it's still invalid, request & test again, etc. See how that
forms a loop? It would look something like this:

get input
while (input is not valid)
output "error, please use valid input"
get input
end while

-Kevin
 
J

John Carson

Shock said:
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.


The game continues for as long as bank balance is positive and the user
hasn't entered N to quit the game. Accordingly, you need an outer loop

while(BankBalance>0)
{
// stuff
}

which you can break out of if 'N' is entered.

Inside this, you need an inner loop that gets the wager and keeps prompting
again if the wager exceeds the bank balance:

cout << "Please enter a wager " << endl;
cin >> Wager;

while (Wager > BankBalance)
{
cout << "Wager exceeds Bank Balance. Re-enter wager!" << endl;
cin >> Wager;
}

Once past this loop, you know that the wager is valid and you can proceed to
the next step.
 
J

Jon Bell

[...] 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...} [...]

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.

That is, if the wager is greater than the bank balance, you're supposed to
ask the user to enter the wager again (presumably after telling him/her
why the previous attempt was invalid), then check *that* wager to see if
it's valid, and if not, ask the user to enter the wager again, etc.
*That's* where the while loop comes in.

The idea is that the program should not proceed beyond this point until
the user finally gets the idea and enters a wager that is less than
his/her bank balance.
 
S

Simon Turner

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

That makes sense on the face of it, but you need to think about how it
behaves in the face of user input. The idea is that you:

1 - prompt the user to enter their wager
2 - check the wager isn't more than they have in the bank
3 - if the wager is ok (according to 2), let it proceed
4 - if they bet more than they have, you need to tell them they bet too
much, and let them try again (ie, go back to step 1).
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.

Thanks for including the question, and the whole source. That makes it a
lot easier.
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.

Look at the numbered steps above, and see how they could fit a while
loop. You want to keep prompting the user and reading their input until
they give you a valid wager.
The key thing is that you don't _expect_ them to keep entering duff
wagers, but you need to handle it gracefully of they do.
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;

The two lines above should probably be inside the loop, unless you want
to use a different prompt if the user gets it wrong the first time.
while ( Wager <= BankBalance ) {

This is false if the user made a mistake, so they will never get another
chance.
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;

Here, you ask them to try again, but don't give them any opportunity.
} // 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
<snip other functions>

Consider splitting the input out into a seperate function, so you can
have a simple

Wager = get_wager(BankBalance);

in your main() function, and worry about verifying the user input
seperately. You can simplify main() by deciding that get_wager will only
return a valid wager.

Consider what a typical session should look like when thinking about the
contents of get_wager(), eg.

craps> Enter your wager (current balance is 1000)
user> 2000
(oops, I meant to type '200')

craps> You can't bet more than you have: wager must be <= 1000
craps> Enter your wager (current balance is 1000)
user> 200

(this time the while loop exits, and the value 200 is returned from
get_wager)

If you want to solve this yourself, then the example code below is a spoiler.

--- spoiler ---

int get_wager(int balance)
{
int wager;
do
{
cout << "\nEnter your wager (current balance is "
<< balance << ") ";
cin >> wager;

if( wager > balance )
cout << "\nYou can't bet more than you have: wager must be <= "
<< balance;

} while( wager > balance );

return wager;
}

--- /spoiler ---
Thanks again!

Shock56

You might also want to write an example session for the program as a
whole, thinking about what you'd like to see, before you think about the
rest of it. Consider having the user enter bad values accidentally quite
often, since you'll have to make sure that you check your input and
handle it appropriately.


HTH,
Simon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top