0

I'm trying to do the CS50 cash problem and the answer keeps getting number 1 no matter which change I input. It won't accept negative numbers as asked by the problem, but I don't know why it doesn't work for positive numbers. I really can't see where's is my mistake and I'm trying to see it since yesterday, I'm even comparing with other codes I found online and I can't see nothing. Please help me learn.

Here's my code

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

int main (void)
{
    int coins=0;
    int quantity=0;

    float change;  
    do  
    {  
        change = get_float("change owed = ");  
    }while (change < 0); 

    quantity = round (quantity * 100); 

    while (quantity >= 25)
    {
        quantity-= 25 ;
        coins ++;
    }

    while (quantity >= 10)
    {
        quantity -= 10;
        coins++;        
    }

    while (quantity >=5)
    {
        quantity -= 5;
        coins++;        
    }

    while (quantity>=1)  
        quantity -= 1;
        coins++;


    printf("%i\n", coins);

}
1
  • Are you sure this quantity = round (quantity * 100); is what you meant? Mar 30 '20 at 17:57
1

The problem that you're seeing is here:

quantity = round (quantity * 100); 

So, what is the value of quantity before this line execute?

What variable actually contains the value of the change to be counted?

There's another issue in the code, but it's an easy fix. I'll let you find that after you fix this. ;-)

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

You must log in to answer this question.

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