Search code examples
ccs50greedy

Do-While loop is working but not getting final output


The purpose of this code is to ask for the change required and then output the smallest number of coins that can be used to provide that change.

Not sure why my code is not outputting anything?

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

float get_defined_float(string prompt);

int main(void)

{
    float f = get_defined_float("Required Change: ");
}

float get_defined_float(string prompt)
{
    float n;
        do
        {
            n = get_float("Required Change: ");
        }
    while (n<=0);
    return n;




    int input = round(n * 100);
    int quarters = input / 25;
    int dimes = (input % 25) / 10;
    int nickles = (input % 25 % 10) / 5;
    int pennies = (input % 5) / 1;


    int total = quarters + dimes + nickles + pennies;
    printf("%d", total);
}

Solution

  • Your return statement is above the output printf statement in your program. That means the last half of get_defined_float is dead code - it is never run.

    It seems like you probably want to take that dead code and move it into your main routine, subsituting f for n in the int input = round(n * 100); statement.

    You may also need to add a newline (\n) to that output statement in order to flush the output buffer (normally line buffered for TTYs).

    Turn on some compiler warnings!