繁体   English   中英

“错误:此处不允许 function 定义”在 C 中有大括号?

[英]“error: function definition is not allowed here” w/ curly brackets in C?

您好,我正在尝试为其中一位小纸算命先生创建一个在线表格。 这是 WIP 代码,我大部分时间都在调试它,但我不断收到这两个错误

fortune.c:58:3: error: function definition is not allowed here
fortune.c:79:4: error: function definition is not allowed here

我查了很多这个问题,它一直告诉我你必须在 main 之外定义你的函数,因为它们是全局的或其他的,但我已经在主 function 之外调用了它们,并且在它开始之前,前几行。 我也尝试过将整个代码放在顶部,但这样对我来说看起来很难看,并且会导致 20 个错误。 我只是松了一口气,把所有的错误都降到个位数,哈哈。 所以这里的代码任何帮助将不胜感激。 这是我第一次编码。 砖砌

#include <stdio.h>
#include <string.h>  

void fortune1(void);
void fortune2(void);

int main (void)
{
    
int fort; 
int tune;
    
// get color
char color; // []?
printf("Color?\n");
scanf("%s", &color);
int count;
count = strlen(&color);  
{
if(count % 2 == 0) 
    int num1; 
    printf("Number?\n");
    scanf("%d", &num1);
        if(num1 % 2 == 0)
        {
            fortune1(); 
        }
        else
        {
            fortune2();
        }
}
{   
    int num2; // []?
    printf("Number?\n");
    scanf("%d", &num2); 
        if(num2 % 2 == 0)
        {
            fortune1();
        }
        else
        {
            fortune2();
        }
}
}


// fortune1 function
void fortune1(void)
  {                   
        do
        {
            int fort;
            printf("Fortune?\n");
            scanf("%d", &fort);
        } while (fort < 5 || fort > 0); //must be in range 1-4
            if(fort = 1);
                printf("fortune1");
            if(fort = 2);
                printf("fortune2");
             if(fort = 3);
                printf("fortune3");
            if(fort = 4);
                printf("fortune4");
    }

    
// fortune2 function

void fortune2(void)
   {                
        do
        {
            int tune;
            printf("Fortune?\n");
            scanf("%d", &tune);
         } while (tune < 9 || tune > 4)
            if(tune = 5);
                printf("fortune5");
            if(tune = 6);
                printf("fortune6");
            if(tune = 7);
                printf("fortune7");
            if(tune = 8);
                printf("fortune8");
    }
}

'''
#include <stdio.h>
#include <string.h>  

void fortune1(void);
void fortune2(void);

int main (void)
{

    int fort; 
    int tune;

    // get color
    char color[10]; // []? if you're using string, then use array
    printf("Color?\n");
    scanf("%s", &color);
    int count;
    count = strlen(color);  //if u use &, you'll count the variable address
    
    if(count % 2 == 0) 
    {
        int num1; 
        printf("Number1?\n");
        scanf("%d", &num1);
            if(num1 % 2 == 0)
            {
                 fortune1(); 
            }
            else
            {
                 fortune2();
            }
    }
    else
    {
        //i dunno if this part is inside if or else 
    int num2; // []?
    printf("Number2?\n");
    scanf("%d", &num2); 
        if(num2 % 2 == 0)
        {
            fortune1();
        }
        else
        {
            fortune2();
        }
    }
}


// fortune1 function
void fortune1(void)
  {         
        int fort;          
        do
        {
            printf("Fortune1?\n");
            scanf("%d", &fort);
        } while (fort >= 5 || fort <= 0); //must be in range 1-4
        // i change the condition iniside while, because if not, the code will always looping
        if(fort == 1) // == not =
            printf("fortune1");
        if(fort == 2)
            printf("fortune2");
        if(fort == 3)
            printf("fortune3");
        if(fort == 4)
           printf("fortune4");
        //don't use ; after if
    }


// fortune2 function

void fortune2(void)
   {     
        int tune;           
        do
        {
            printf("Fortune2?\n");
            scanf("%d", &tune);
         } while (tune >= 9 || tune <= 4); //; after while in do while
         if(tune == 5)
            printf("fortune5");
         if(tune == 6)
            printf("fortune6");
         if(tune == 7)
            printf("fortune7");
         if(tune == 8)
            printf("fortune8");
    }

如果我错了,请告诉我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM