0

I'm trying to set up my solution to the Cash problem using modulo. The input seems to be working correctly so far. It only accepts positive numbers through the do..while loop.

The problem I think I am running into is the actual algorithm. I have it set up like this:

while (amount>=25)
   amount2=amount%25;

for quarters. Now I can add variables and keep going like this with dimes, etc. But does my program just stop after this? How can i get it to continue if amount is LESS than 25?

1

You have created an infinite loop. If the loop starts, it will never exit the loop. It tests the value contained in amount to decide to continue or exit, but since the value of amount is not changed inside the loop, it will never exit. You either need to change the value of amount inside the loop or you need to use an if statement instead, so that it only executes once.

The assignment part of the statement, amount2=amount%25 counts the number of quarters. It doesn't deduct the value of those quarters from amount. You need to use curly braces {} to create a code block that will do both of those steps.

In addition, for your example, if it is less than 25 cents, it would skip the while loop because the test condition failed, and just move on to the next line of code.

But, if you think about it carefully, you should be able to do both steps without using an if or a while loop. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • ooh, I wonder if anyone caught the error in my answer? ;-) Well, an erroneous statement, but it doesn't really affect the answer to the question! haha. If you did, just say that you caught it. Don't point it out! – Cliff B Nov 27 '18 at 5:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .