1

Not sure what the issue is, ive gotten this in all programs ive written with a function Would appreciate help

Error message: cash.c:33:5: error: function definition is not allowed here

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

// program uses a greedy algorithym to print the minimum number of coins required to give someone change

int minimum_coins(void);

int main(void)
{
// do while loop to prompt the user for positive value of change
    float dollars;
    do
    {
        dollars = get_float("Change owed: ");
    }
    while (dollars < 1);
    int cents = round(dollars * 100);
    
    
    
    // calls function, assigns the needed coins to i
    {
        int i = minimum_coins();
        printf("%i\n", i);
    }
    
    
    
    // function gets the minimum number of coins needed; 
    
    int minimum_coins(void)
    {
        int coins_needed = 0;
        while (cents >= 25)
        {
            cents = cents - 25;
            coins_needed++;
        }
        while (cents < 25 and >= 10)
        {
            cents = cents - 10;
            coins_needed++;
        }
        while (cents < 10 and >= 5)
        {
            cents = cents - 5;
            coins_needed++;
        }
        while (cents > 0 and < 5)
        {
            cents = cents - 1;
            coins_needed++;
        }
        return coins_needed; 
    }
}
2

It's a simple fix. Functions cannot be placed inside of main.

Just move the closing curly brace at the end to just above the line with the following comment:

// function gets the minimum number of coins needed; 

This will close main() properly and allow functions to follow.

That should resolve this problem. Not saying there aren't other issues though.

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

You must log in to answer this question.

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