Coin game

This program will play a game with the user. The goal of the game is to use different numbers of US Coins to match a dollar amount that the user specifies. The user wins if they can give you a correct breakdown of an amount of half dollars, quarters, dimes, nickels, and pennies that adds up to whatever amount they specify.

Write a program that does the following:

Welcome the user to the game. This should be done only once, at the beginning of the program.

Then, enter into a LOOP, that does the following, in order:

  • Present the user with the following menu:p – play
    s – stats
    q – quit
    choice: 
  • Allow the user to enter a menu entry.

Your program should accept both UPPER and LOWER case letters as valid menu entries. Ex: ‘P’ and ‘p’ should both play the game.

  • Now, DETERMINE what selection the user made. The selection options are either P S Q or some Invalid Option:

PLAY:

Prompt them for a dollar amount (as a double) that they will attempt to match with their coin entries.

DO NOT LET THE USER PROCEED if they enter a negative target amount. You can use a loop here to Force the user to retry should they make a bad entry (see sample run 3).

Now, allow the user to enter an integer amount for the number of half dollars, quarters, dimes, nickels, and pennies that they’d like.

  • If any of the values they enter for the number of each coin is negative, display an error message and count that game as a loss. Otherwise, check to see if the user won the game. Add up the total amount of $ they entered and compare it against the value they entered when the program began.
  • If the amount matches the target amount, they won! Print out: “YOU WIN!” and count the game as a win. If they lost, print out “YOU LOST by: ” and then the amount of $ that they were off by, and count the game as a loss.
  • STATS:

Print the number of wins and losses to the screen as seen in the sample runs.

  • QUIT:

Print out the final statistics which include the wins, losses, and the Final Win Percentage.

The final win percentage must be printed to 2 decimal places. 

  • If the user quits the game without having played any games, print the “guess you’re scared” messaged seen in sample run 5.
  • SOMETHING ELSE OTHER THAN PLAY, STATS, or QUIT:

If the user enters an invalid character that is not on the menu, your program should print an error message as seen in the sample run 2.

  • Your overall loop should now conclude, and should restart if the user selected anything other than the quit option.
  • Hints

As you go through the program, you will have to keep track of the number of wins and losses. Don’t forget to begin the variables representing these counts at 0 at the beginning of your program.

  • Remember integer math and what that can do, especially when performing division!
  • a “do – while” loop is ideal here, in my opinion, for the overall looping of the program. This is because we are performing a pattern of: show menu to user, gather user’s value, determine value, and then *check to see if we do this all over again*. Since this *check* is at the END of this pattern, a do-while fits naturally here. You can though use any kind of loops you’d like. I would NOT recommend using for loops in this program.

Your program will NEED to utilize at least 2 loops, the previously mentioned one which loops the menu/gameplay, as well as one during gameplay which forces the user to retry should they enter a negative target amount.

Rules

  • If the user loses the game, the difference (amount they were off by) should neverdisplay as a negative value.
  • Don’t allow your program to divide by 0. This can happen when the user quits without having played any games.

When error checking you DO NOT need to account for the user entering a different data type than the one expected. For example you do not need to worry about the user entering a character when entering the target amount to match when playing the game.

All dollar values printed must be printed in dollar format $dd.cc (to two decimal places).

  • You may ONLY HAVE ONE return statement which is the last line of main() in each of your mini-programs. No exit statements are permitted, either.

Declare ALL of your variables at the beginning of main(), not scattered throughout.

  • Sample Runs
  • (user input is underlined, to distinguish it from output) 
  • Your output format should look the same as mine. Keep the spacing and symbols the same. It’s ok if your words/capitalization differ a bit.

Sample run 1

Welcome to the coin game!
p – play
s – stats
q – quit
choice: p

Enter target amount $dd.cc: 2.00
Okay! You need to match the value of $2.00
Enter the number of:
Half Dollars: 2
Quarters: 2
Dimes: 5
Nickels: 0
Pennies: 0
You WIN!

p – play
s – stats
q – quit
choice: p

Enter target amount $dd.cc: 1.50
Okay! You need to match the value of $1.50
Enter the number of:
Half Dollars: 0
Quarters:  0
Dimes:  0
Nickels:  0
Pennies:  150
You WIN!

p – play
s – stats
q – quit
choice: s

Wins: 2
Losses: 0

p – play
s – stats
q – quit
choice: q

Final stats:
Wins: 2
Losses: 0
Final Win Percentage: 100.00%

  • Sample run 2
  • Welcome to the coin game!
    p – play
    s – stats
    q – quit
    choice: P

    Enter target amount $dd.cc: 2.45
    Okay! You need to match the value of $2.45
    Enter the number of:
    Half Dollars: 3
    Quarters: 8
    Dimes: 10
    Nickels: 5
    Pennies: 0
    You LOST by: $2.30

    p – play
    s – stats
    q – quit
    choice: s

    Wins: 0
    Losses: 1

    p – play
    s – stats
    q – quit
    choice: x
    Invalid option.

    p – play
    s – stats
    q – quit
    choice: p

    Enter target amount $dd.cc: 1.11
    Okay! You need to match the value of $1.11
    Enter the number of:
    Half Dollars: 0
    Quarters: 4
    Dimes: 0
    Nickels: 0
    Pennies: 11
    You WIN!

    p – play
    s – stats
    q – quit
    choice: S

    Wins: 1
    Losses: 1

    p – play
    s – stats
    q – quit
    choice: Q

    Final stats:
    Wins: 1
    Losses: 1
    Final Win Percentage: 50.00%

Sample run 3

Welcome to the coin game!
p – play
s – stats
q – quit
choice: p

Enter target amount $dd.cc: -7.00
Cannot be negative. Try again: -9.30
Cannot be negative. Try again: -78.32
Cannot be negative. Try again: -1.23
Cannot be negative. Try again: 100.00
Okay! You need to match the value of $100.00
Enter the number of:
Half Dollars: 200
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0
You WIN!

p – play
s – stats
q – quit
choice: q

Final stats:
Wins: 1
Losses: 0
Final Win Percentage: 100.00%

  • Sample run 4
  • Welcome to the coin game!
    p – play
    s – stats
    q – quit
    choice: p   

    Enter target amount $dd.cc: 4.50
    Okay! You need to match the value of $4.50
    Enter the number of:
    Half Dollars: -2
    Quarters: 1
    Dimes: 9
    Nickels: 5
    Pennies: 3
    You can’t enter a negative amount of coins. You lose.

    p – play
    s – stats
    q – quit
    choice: p

    Enter target amount $dd.cc: 4.50
    Okay! You need to match the value of $4.50
    Enter the number of:
    Half Dollars: -1
    Quarters: -2
    Dimes: -3
    Nickels: -4
    Pennies: -5
    You can’t enter a negative amount of coins. You lose.

    p – play
    s – stats
    q – quit
    choice: s

    Wins: 0
    Losses: 2

    p – play
    s – stats
    q – quit
    choice: q

    Final stats:
    Wins: 0
    Losses: 2
    Final Win Percentage: 0.00%

  • Sample run 5
  • Welcome to the coin game!
    p – play
    s – stats
    q – quit
    choice: q

    Final stats:
    Wins: 0
    Losses: 0
    Final Win Percentage: 0.00%
    Guess you’re scared! Come back when you’re ready to play!

Additional Program Rules

No global variables

All input and output must be done with streams, using the library iostream

  • You may only use the iostream, iomanip, and cmath libraries

Include the appropriate header comment the top of your file. This is provided on the canvas course homepage.

Coin game

We offer the best custom writing paper services. We have answered this question before and we can also do it for you.

GET STARTED TODAY AND GET A 20% DISCOUNT coupon code DISC20

Leave a Comment