0

The problem is that it works for some values and not for others even though the float value is rounded. pls help

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

int coins = 0;

int cents = round (change * 100);

//here starts the action
if (cents > 25)
{
    do
    {
        coins++;
        cents = cents - 25;
    }
    while (cents > 24);
}

if (cents < 25 && cents > 10)
{
    do
    {
        coins++;
        cents = cents - 10;
    }
    while (cents > 9);
}

if (cents < 10 && cents > 5)
{
    do
    {
        coins++;
        cents = cents - 5;
    }
    while (cents > 4);
}

if (cents < 5 && cents > 1)
{
    do
    {
        coins++;
        cents = cents - 1;
    }
    while (cents > 0);
}
//here finishes the action
do
{
printf("you will need %i coins\n", coins);
}
while (false);
}````



0

The problem lies in the if statements. For example: if (cents > 25) What happens if cents == 25? It will skip this section even though there's still a quarter in there!

On a side note, think about this. Instead of an if/do-while, why not just do a standard while loop?

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

| improve this answer | |

You must log in to answer this question.

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