![](/img/trans.png)
[英]Getting an error in C saying that function definition is not allowed here before '{' token
[英]“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.