为什么我的goto命令在编译时会出现问题?

时间:2015-11-21 14:54:29

标签: c goto

我为数学动作编写了一个小程序,但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实际上正在工作。

3 个答案:

答案 0 :(得分:3)

问题是你有

#define YorN
#define End
#define Start

在你的来源的开头。这些预处理器宏将在编译为空之前进行评估,因此代码为:

YorN:
goto YorN;

将成为

:
goto;

只需删除它们,因为拥有它们毫无意义。

答案 1 :(得分:0)

请勿#define您的转到标签。您的代码也缺少分号(在标scanf("%c",&tryAgain)YorN之后)。

Here您可以看到更正的版本。

为了完整起见,我将在此处发布更正后的代码:

#include<stdio.h>

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

答案 2 :(得分:0)

它不会编译,因为你已经#defined目标符号不存在。

其他问题: 在大多数printf()格式字符串中,您使用%d而不关心参数的实际类型。将对象投射到int或使用正确的类型(%fdouble)。

最后一个问题:对于您正在做的事情,您应该使用功能循环。你的每个人都可以用适当的循环替换。

希望这有帮助。