0

I don't know what is wrong with my code. I keep inputting 0.41 and getting 8 as the answer.

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

int main(void)
{
float x;


do
{ 
    printf("How much change is owed? ");
    x = GetFloat ( );
}
while (x < 0);

int y = roundf(x*100);
int z = 0;

while (y >= 25)
{
   z++;
   y = y - 25;
}

while (y >= 10)
{
    z++;
    y = y - 10;
}

while (x >= 5)
{
    z++;
    y = y - 5;
}

while (y >= 1)
{
    z++;
    y = y - 1;
}

printf("%d\n", z);

}

1 Answer 1

0

This is a perfect example of why not to use single letter variable names. When naming variables, the best practice is to give each variable a brief but self descriptive name, like change, penny, nickel, dime, coins. These kinds of names go a long way to help people understand your code when they read it, or worse, when they have to modify it. (Take a look at your own code in six months and try to understand it. ;-) ) It also does a lot to make sure that the right variable is used.

When you use variable names that are too short, like single letters (other than for loop counters), it's easy to make a mistake, partly because the var name isn't self-descriptive.

With that in mind, do you see anything wrong with this?

            while (x >= 5)

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 .