首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“错误:这里不允许函数定义”C中的w/花括号?

“错误:这里不允许函数定义”C中的w/花括号?
EN

Stack Overflow用户
提问于 2020-07-21 03:53:55
回答 1查看 216关注 0票数 0

你好,我正在尝试创建一个在线形式的一个小纸面算命员。这是WIP代码,我已经对它进行了大部分调试,但是我一直收到这两个错误

代码语言:javascript
复制
fortune.c:58:3: error: function definition is not allowed here
fortune.c:79:4: error: function definition is not allowed here

我已经查了很多这个问题,它一直告诉我,你必须在main之外定义函数,因为它们是全局的,或者其他什么的,但是我已经在主函数之外,在它开始之前,用前几行调用了它们。我还试着把整个代码放在最上面,但在我看来,它看起来很难看,并导致了20个bug。我只是松了一口气,把所有的错误降到个位数lol。因此,这是代码,任何帮助都将不胜感激。这是我第一次编码。砖块

代码语言:javascript
复制
#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");
    }
}

'''
EN

回答 1

Stack Overflow用户

发布于 2020-07-21 05:06:57

代码语言:javascript
复制
#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");
    }

如果我错了请告诉我

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63007101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档