简体   繁体   中英

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

hello i'm trying to create an online form of one of those little paper fortune tellers. this is the WIP code and I've mostly debugged it but I keep getting these two errors

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

ive looked up the question a lot and it keeps telling me that you have to define your functions outside of main because they're global or whatever but i already invoked them outside of the main function and before it begins, with the first couple lines. i've also tried just putting the whole code towards the top but it looks ugly to me like that and it causes like 20 bugs. i was just relieved to get all the errors down to single digits lol. so here's the code any help would be appreciated. this is my first time coding. brick'd

#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");
    }

please tell me if i'm wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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