shock56@charter.net (Shock) wrote in message news:<ef0f2e3d.0309091856.8658a08@posting.google.c om>...[color=blue]
> 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...}[/color]
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).
[color=blue]
>
> 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.[/color]
Thanks for including the question, and the whole source. That makes it a
lot easier.
[color=blue]
>
> 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.[/color]
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.
[color=blue]
> 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;[/color]
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.
[color=blue]
>
> while ( Wager <= BankBalance ) {[/color]
This is false if the user made a mistake, so they will never get another
chance.
[color=blue]
> 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;[/color]
Here, you ask them to try again, but don't give them any opportunity.
[color=blue]
> } // 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
>[/color]
<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 ---
[color=blue]
>
> Thanks again!
>
> Shock56[/color]
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.