I am totally new to computer sciences and I have been working on the greedy.c problem for quite some time now. While my script calculates the coin numbers correctly, the loop I have in the beginning of the script to check the condition where input is greater than 0 doesnt seem to work. I have tried "do while" and "while" loops. Since do while loop has to execute at least once, it probably doesn't fit the task here. But while loop should. However, when I enter correct numbers, it works whereas when I enter for example -1, it jumps back to terminal. My partial code:


int main(void)
    printf("How much change is the customer owed?:");
    float f = GetFloat();
    while (f >= 0)
        float n = roundf(f*100)/100;
        int x = (int)(100*n); 
        /*printf("Amount: %i\n", x);    amount of cents in integer form*/

        int y = (int)(x/25);  
        /*printf("q%i\n", y);   number of quaters*/

        int z = (x%25);
        /*printf("%i\n", z);   remainder of quaters*/
    return 0;
  • I do not know why the codes displayed are so strange. But It should not matter, the dotted line just represents the part I left out. – Yinzhou Zhu Feb 4 '15 at 21:58
  • The return 0 statement is placed inside the loop, so it works only till return statement is not encountered, once it encounters return statement, then it exits. You probably wish to put it just before the last brace and not where you have placed it currently. – sinister Feb 5 '15 at 0:06
  • If I move return 0 to right before the last curly brace the code inside the loop keeps executing though. It will just prints out the number constantly. I thought by putting the return 0 inside, it stops the loop once it reaches it in the first run, which is what I hoped to achieve. – Yinzhou Zhu Feb 5 '15 at 0:12
  • To my understanding, the while loop should check the condition before executing, is that right? – Yinzhou Zhu Feb 5 '15 at 0:14
  • I see my logic flaw here, maybe while loop is not what I should be using here? I really can't think of anything else... – Yinzhou Zhu Feb 5 '15 at 2:20

You want to first get your input correct.

while f<=0
     f = getfloat()

then you want to worry about the change with one or more other while loops or the maths steps that you are suggesting.

then at the end return 0.

If that doesn't help, feel free to ask more, I will give you more hints.

  • When I try to write while (f<=0) float f = GetFloat(); It gives an error: use of undeclared identifier 'f' It probably means I have to initialize it before the while loop. But how do I do that is what I haven't been able to find out. It seems that I have to give a fixed number or 0? – Yinzhou Zhu Feb 5 '15 at 4:25
  • That's easy. you add: float f = 0; in the line before the while loop. This tells the compiler that f is a float. And in the while loop you only use f = getfloat(), not float f = getfloat() – gitterb Feb 5 '15 at 15:05

If I'm reading your code correctly, there is no way for the prompt to ever get re-executed after a false condition is realized. It is outside of any looping construct. The printf executes, the getfloat executes, and then either the loop is entered or the entire loop is bypassed.

  • Do you suggest I move the GetFloat command into the while loop brace? How do I initialize the float f outside the loop without giving a fixed value, if possible? – Yinzhou Zhu Feb 5 '15 at 1:46
  • I highly recommend re-watching the walkthrough for Mario. Zamyla covers the use of a "do while" loop and how it can be used to prompt for and validate user input. A "do while" loop will always execute the "do" at least once prior to evaluating the condition. – KernelRutgers Feb 5 '15 at 5:59

Use a Do While loop. The Do will contain the prompt and Getfloat command, the While will include the conditions you want for it to return to the Do if it fails. (Remember in a Do While Loop the conditions (aka the While) come after the commands (aka the Do). Hope this helps!!

You must log in to answer this question.

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