Loop Giving Change
To conclude the change calculator we need to make this so that the user has the option to calculate another change value. The logic for this is shown in the following flow chart.
-
Add a new
again
variable, write out a prompt, and read in the new value at the end of the current code. -
Add in a
do...while
loop that will repeat whileagain
is not equal “N” or “n”.Go ahead and try implementing this. Make sure the
do while
loop, captures the code that reads the values from the users, as well as the code that outputs the change values.Compile, run, and test. Make sure that you can loop multiple times, and exit when you want. Test the program, and see if you can spot any issues.
-
-
To complete the program, we can need to check if the user has paid enough. If they have not paid enough, then we should output a message and see if they want to calculate change again.
Have a go at this yourself. There are several ways you can achieve this.
-
In this solution we use a
continue;
statement to jump to the condition of thedo ... while
when there is insufficient funds.
-