0

I've been trying to solve this problem for a while but I am completely stuck, so thought I'd ask for help.

This is what I have so far:

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

int main(void)
{

 float dollars, coins;
 int cents, a, b, c, d;

 do
 {
    printf("Hi! How much change do you need? ");
    dollars = get_float();
 }
 while (dollars <= 0);

 (cents = dollars * 100);
 (coins = 0);

    do
    {
        (a = cents - .25);
        (coins++);
    }
     while (cents >= .25);

    do
    {
        (b = a - .10);
        (coins++);
    }
    while (a >= .10);

    do
    {
        (c = b - .05);
        (coins++);
    }
    while (b >= .05);

    do
    {
        (d = c - .01);
        (coins++);
    }
    while (c >= .01);

 printf("%f\n", coins);
}

I thought my logic was sound--I created different variables for each step of subtracting different coin values and then had the coin number go up by 1 each time.

When I do make greedy, it works with no error. When I then start the program it prompts the user correctly.

however, as soon as I put in a value, the system seems to stall. It doesn't print a value, but it also doesn't reset or prompt me for anything else, it just indents to the next line and then doesn't move, and the ~/workspace/pset1/ does not come back up.

What am I doing wrong? When I try to do the check next to all the frowny faces it says "killed by the system" someone please help! Thanks!!

1

First of all, the problem with killed by the system is due to your program being stuck in an infinite loop.

When I get into such an issue it helps me to take out a pen and paper and follow the variables through the program and see what happens. Its a form of debugging. You can also learn to use debug50.

Specifically in your case think about what your loops are doing at each step and whether they ever exit.

| improve this answer | |

You must log in to answer this question.

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