0

I tried to use for loops to solve the cash (less) problem set, not sure if thats possible. Can this code be fixed or do I need to use while loops instead? the code compiles, but the output is either (input)*100+2, or it gets stuck in an infinite loop.

# include <stdio.h>
# include <cs50.h>
# include <math.h>

int main (void) {
  
      //ask for change
    float c;
    int i = 0;
    do
    {
        c = get_float("Change owed: \n");
    }
    while (c < 0.00);
    int d = round(c * 100);
    
    //check how many quarters can be used
    for ( ; d > 25 ; d = d - 25, i++)
          
          //check how many dimes can be used
           for ( ; d > 10 ; d = d - 10, i++)
           
           //check how many nickles can be used
               for ( ; d > 5 ; d = d - 5, i++)
           
           //check how many pennies can be used
       for ( ; d > 1 ; d = d - 1, i++);
       
       //display total ammount of coins used
     if (d <= 0) {
        printf("%i\n", i);
     } 
       
}     
1

You need a semicolon immediately after each for loop. Otherwise, all the statements until the next semicolon are considered the body of that for loop. The compiler will complain if you put a semicolon on the same line as an empty-body, so it needs to be by itself on the next line.

https://www.quora.com/What-does-a-for-loop-without-curly-braces-do

Also, you need to check if d is greater than or equal to the coin values, not just greater than. Otherwise, your program is unusually written, but it seems to work correctly!

If this answers your question, please click on the check mark to accept. Don't be shy to ask another question the next time you have an issue!

| improve this answer | |
  • Thank you so much for the help! I'm glad the code works, I'm going to clean up further! – Angel Aug 27 at 23:34
1

Normal practice is to use a for loop when the number of passes is well defined, either by a hard coded value or by a value contained in a variable. When the number of passes is based on a condition or a non-well-defined value, a while loop is preferred.

Having said this, if you want to continue your approach, it is doable. You might want to try to write it both ways, just for the practice!

But think about this. The for loops are configured as nested for loops because they are not closed out with semicolons (except for the last one.)

Some tips.

What happens if and when d=25? or 10 or 5?

Do you really need a for loop to count pennies?

At the end, if d>0, what will the program do? Also, will d ever correctly be less than 0, or does that indicate something went wrong? What should the program do if it ends and d != 0? Should it just end without printing anything? Or should it print the number of coins? Or should it print an error message?

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

| improve this answer | |
  • Thank you so much for the insight! I'm certainly going to try it out as a while loop too. – Angel Aug 27 at 23:33

You must log in to answer this question.

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