-3

**I've done a few changes and i'd just like to know if you can help me find out why it isn't running. When I run it the text is printed but the input never stops running. ** The program is meant to replicate(?) the greedy algorithm. It first asks the user how much change is owed and then provides the minimum number of coins with which said change can be made.

I'd greatly appreciate any help you can give with my code.


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

int main(void)

{
    //declared variables
    float change;
    int num = 0;

    // prompts for amount owed

    printf("Please provide the amount owed: ");
    change = GetFloat();
    while (change != 0)

        change = change * 100;
    int owed = round(change);

    {
        // if statements for calculating number of coins required
        if (owed >= 25)
        {
            owed = owed - 25;
            num ++;
        }
        if (owed >= 10 && owed < 25)
        {
            owed = owed - 10;
            num ++;
        }
        if (owed >= 5 && owed < 10)
        {
            owed = owed - 5;
            num ++;
        }
        if (owed >= 1 && owed < 5)
        {
            owed = owed - 1;
            num ++;
        }
    }
    printf("%d\n", num);
}

When I alter the while statement and comment out the 'change = change * 100;' it starts to work. However, in doing so the round function won't execute.

  change = GetFloat() * 100;
    while (change != 0)

    // change = change * 1000;
    // int owed = round(change);
thelaws
  • 7,991
  • 6
  • 35
  • 54
  • `while (change > 0)` makes no sense. You'll get infinite loop doing rounding if condition is true. And no rounding at all if it's false. And rounding result is unused and thus lost. – user694733 Jun 22 '16 at 12:08
  • 1
    Someone answered this here: http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c – Michael Jun 22 '16 at 12:13
  • Why would you round it to an int? Casting.478 to int gives you 0, because there is no integer value between 0 and 1. – Ken White Jun 22 '16 at 12:46
  • Provide the minimal reproducible example. If round works not as intended, remove anything not relevant to round. Provide what do you receive, and what do you expect. – user31264 Jun 22 '16 at 13:24
  • I see that your code contains bugs which has nothing to do with round. It is your job to find them. Debug your code. – user31264 Jun 22 '16 at 13:26
  • Adding a couple of lines that explain what the program is meant to do, instead of letting us figure it out, would be nice. Ok, this is a very simple program, but it doesn't mean you have to make it harder for us. Also, read about a [mcve]. – Fabio says Reinstate Monica Jun 22 '16 at 13:35
  • Really sorry i'm frustrating you guys, i'm pretty new to coding in C and just confused at the moment. I obviously haven't yet developed the mind set of a programmer but hopefully i'll get there. I'll edit in what its meant to do. – TriggerFinger Jun 22 '16 at 14:06
  • Please don't remove the entire body of your question. It makes the comments & answers meaningless – thelaws Jun 22 '16 at 16:15
  • Is it possible then to delete the post through mod intervention. The problem has been solved and I don't think that it'll serve any further purpose. – TriggerFinger Jun 23 '16 at 11:27

1 Answers1

1

To my understanding, the part

while (change != 0)
    change = change * 100;

is an infinite loop if it is started when change is nonzero. Depending on the platform, eventually NaN or positive infinity is reached by the multiplication, causing the loop to continue forever.

Codor
  • 17,447
  • 9
  • 29
  • 56