簡體   English   中英

需要幫助解決涉及 c 語言中“此處不允許函數定義”的問題

[英]Need help solving a issue involving 'function definition is not allowed here' in c language

嘗試在 C 中運行它,但在引用“{”時它一直說“這里不允許函數定義”。 這是我試圖運行的完整代碼,它被稱為“貪心算法”,所以我仍在學習如何制作它。 該程序應該為您提供確切的零錢,然后讓您知道需要多少硬幣類型。

#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++
        }
    }   
}

您必須在主 function 之外定義您的greedy() function ,如下所示:

#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++
    }
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM