0

this is my first question here so sorry if I do it wrong. My code compiles (it´s not finished yet), and it prompts, but when I run ./cash and type a number, It always gives me 0 coins, no matter what number I type in.

This is my code so far:

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

int main (void)

{
    // Get change owed
    float owed;
    do
    {
        owed = get_float("Change owed: ");
    }
    while (owed < 0);
    
    //Convert dollars into cents
    {
    int cents = round(owed * 100);
    {
        printf("%i\n", cents);
    }
     //Track of change still owed
    //Track of coins used
    int coins = 0;
    float quarter = 0.25;
    float dime = 0.10;
    float nickel = 0.05;
    float penny = 0.01;
    
     while (quarter < coins)
     {
         cents = (cents - quarter);
         coins++;
     }

    {
        printf("Total coins:%i\n", coins);
    }
    }

1 Answer 1

0

I see two issues in this code. First, it's printing 0 all the time because the while loop will never run. It tests to see if quarter (0.25) is less than coins (0 when the loop first starts.) So, the test always fails and the while loop never executes. Maybe there's a different var other than coins that you meant to use? ;-)

Second, the code is working with cents, not dollars. Then why are quarter, dime, nickel and penny all floats and all less than 1?

Lots of hints here. Happy programming! ;-)

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

1
  • Thank you for your response! Really helped me out. Commented Mar 1, 2021 at 7:56

You must log in to answer this question.

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