Back to Challenge Set 8

Code Challenge: Piggy Bank

Difficulty  

divider

The Brief

What they asked for
I want to track my allowance savings. Let me keep typing in
amounts I put into the piggy bank, and when I type 0 to mean
I'm done, tell me the total.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Nobody knows how many deposits are coming before the program runs. That is the same idea as Penny Doubling in this set, but stopped by a typed value instead of a target.
  • 0 is not a deposit — it is the signal to stop. Do not add it to the total.
  • You need one amount read before the loop starts, so the loop has something to check the first time. Read the next one again at the end of the loop, not the beginning.
  • The total needs to exist before the loop does, starting at 0.

Where to Start

Only one-star challenges have this section. If you already know how to begin, skip it — and when you move up to two stars, working out the order yourself is part of the job.

  1. Declare the total as a double starting at 0.
  2. Read one amount before the loop, and parse it.
  3. Write while (amount != 0), add the amount to the total inside, then read the next amount as the last line in the loop.
  4. Print the total after the loop ends, formatted with :F2.

Sample Output

Sample Output
Amount (0 to stop): 5 [Enter]
Amount (0 to stop): 10 [Enter]
Amount (0 to stop): 20 [Enter]
Amount (0 to stop): 0 [Enter]
Total saved: $35.00

Verified in a real run — 5, 10, and 20 add up to exactly $35.00, and the 0 that stops the loop never gets added.


Submit

Your requirements list and your program file, named cc-piggy-bank.

Commence Challenge