我编写了一个用于数学操作的小程序,但是 goto
在某些情况下命令将无法编译。请帮助我。
这是代码:
#include<stdio.h>
#define line11
#define YorN
#define End
#define Start
int main(void)
{
float userNumber1,userNumber2;
int mathAction;
char tryAgain;
Start:
printf("Please write 2 numbers for your math action(a,b):\n");
scanf("%f %f",&userNumber1,&userNumber2);
Line11:
printf("Please choose 1 of the following math actions by writing the action number :\n"
"1)Adding a+b\n"
"2)Subtracting a-b.\n"
"3)Multiplying a*b.\n"
"4)Dividing a/b.\n");
scanf("%d",&mathAction);
if (mathAction>4 || mathAction<1) //If the user pick something that's not 1-4 , then this if is going on.
{
printf("Grrrr.. pick between 1-4 ! \n");
goto Line11;
}
if (mathAction==1)
{
printf("Adding %d + %d is %d\n",userNumber1,userNumber2,userNumber1+userNumber2);
}
if (mathAction==2)
{
printf("Subtracting %d - %d is %d\n",userNumber1,userNumber2,userNumber1-userNumber2);
}
if (mathAction==3)
{
printf("Multiplying %d * %d is %d\n",userNumber1,userNumber2,userNumber1*userNumber2);
}
if (mathAction==4)
{
if (userNumber2==0)
{
printf("You cant divide %d by 0 , its math error!\n");
printf("Do you want to choose again? y/n\n");
YorN:
scanf("%c",&tryAgain)
if (tryAgain !='y' || tryAgain !='n')
{
printf("Choose only yes or no by y for yes and n for no.");
goto YorN;
}
if (tryAgain=='y')
{
goto Start;
}
if (tryAgain=='n')
{
goto End;
}
}
printf("Dividing %d / %d is %.2f\n",userNumber1,userNumber2,userNumber1/userNumber2);
}
printf("Thanks for using my program !\nDo you want to try again? (Answer y/n)");
scanf("%d",&tryAgain);
if (tryAgain !='y' || tryAgain !='n')
{
printf("Choose only yes or no by y for yes and n for no.");
goto YorN;
}
if (tryAgain=='y')
{
goto Start;
}
if (tryAgain=='n')
{
goto End;
}
End:
system("PAUSE");
return (0);
}
这是编译错误:
q5.c: In function 'main':
q5.c:12:11: error: expected expression before ':' token
Start:
^
q5.c:45:12: error: expected expression before ':' token
YorN:
^
q5.c:69:15: error: expected identifier or '*' before ';' token
goto YorN;
^
q5.c:73:16: error: expected identifier or '*' before ';' token
goto Start;
^
q5.c:77:14: error: expected identifier or '*' before ';' token
goto End;
^
q5.c:79:8: error: expected identifier or '*' before ';' token
End:
^
我已经尝试了一切方法来完成这项工作,请帮忙!
顺便说一句,goto
之一s 实际上正在工作。
最佳答案
问题是你有
#define YorN
#define End
#define Start
在源代码的开头。这些预处理器宏将在编译为空之前进行评估,因此这段代码:
YorN:
goto YorN;
将成为
:
goto;
只需删除它们,因为拥有它们没有任何意义。
关于c - 为什么我的 goto 命令在编译时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33844739/