0

Tried running this in C but it keeps saying "function definition is not allowed here" when referring to '{'. This is the full code that im trying to run, its called a "greedy algorithm" so im still learning how to make it. The program is supposed to give you your exact change back then let you know how many coins of which type will be needed.

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

int main()
{
    int len = 4;
    double cost;
    double pay;
    {
        //promt user for cost
        printf("Enter cost: ");
        scanf("%lf", &cost);
        //prompt user for pay amount
        printf("Enter pay: ");
        scanf("%lf", &pay);
        
        double change = pay - cost;
        
        if(pay > cost)
        printf("Trasncation complete\nHeres your change: %f\n", change);
        else if(pay == cost)
        printf("Transaction complete.\n");
        else if(pay < cost)
        printf("insufficient funds\nStill need: %f", cost - pay);
        //greddy  algorithm
    }
    void greedy(double change)
    {
        int i=o;
        double number;
        double coin[len] = [0.01, 0.05, 0.10, 0.25];
        
        while(i>len)
        {
            if(coin[i] <= change)
            {
                number = change / coin[i];
                printf("%f of %f is needed", number, change);
                change = change * coin[i];
            }
            i++
        }
    }   
}
Simson
  • 3,373
  • 2
  • 24
  • 38
Adonis
  • 11
  • 1
  • 4
  • "*function definition is not allowed here*" That can't be the *first* error if the code is anything like the one posted. Please copy/paste the real code and error(s), do not retype. – dxiv Mar 18 '21 at 05:22
  • Where have you defined variable `len`? Also, you can't (and don't need to) pass length as a variable when you initialize the array with an initialization list. Just use `double coin[] = {0.01, 0.05, 0.10, 0.25}`. – Nikhil Kumar Mar 18 '21 at 05:24
  • @dxiv I updated my post and pasted all of my code in the program. lmk if theres anything i need to do, thx. – Adonis Mar 18 '21 at 05:29
  • 3
    You have the definition of `greedy()` inside the curly braces of the `main()` function just like the error message stated. You can't nest function definitions. – Blastfurnace Mar 18 '21 at 05:30
  • 1
    @Simson You can **declare** a function inside another function. If your compiler lets you **define** it inside a function then it's a [non-standard extension](https://stackoverflow.com/questions/2608158/nested-function-in-c). – Blastfurnace Mar 18 '21 at 05:43
  • @Simson It's non-standard and falls firmly into the "just because you can doesn't mean you should" department. It's not necessary here. The purpose of this feature is extremely esoteric. – tadman Mar 18 '21 at 05:47
  • Adonis, can you clarify if this was a typo error or a misunderstanding about functions? This might be a duplicate question or a simple typo (and therefore closeable). – Blastfurnace Mar 18 '21 at 05:52

1 Answers1

0

You have to define your greedy() function outside of the main function like so:

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

int main()
{
    int len = 4;
    double cost;
    double pay;
    {
        //promt user for cost
        printf("Enter cost: ");
        scanf("%lf", &cost);
        //prompt user for pay amount
        printf("Enter pay: ");
        scanf("%lf", &pay);
        
        double change = pay - cost;
        
        if(pay > cost)
        printf("Trasncation complete\nHeres your change: %f\n", change);
        else if(pay == cost)
        printf("Transaction complete.\n");
        else if(pay < cost)
        printf("insufficient funds\nStill need: %f", cost - pay);
        
    }
     
}
// greddy  algorithm
void greedy(double change)
{
    int i=o;
    double number;
    double coin[len] = [0.01, 0.05, 0.10, 0.25];
        
    while(i>len)
    {
        if(coin[i] <= change)
        {
            number = change / coin[i];
            printf("%f of %f is needed", number, change);
            change = change * coin[i];
        }
        i++
    }
}  
timthedev07
  • 454
  • 1
  • 6
  • 17