如何使用 goto 而不会出现此错误?

Val*_*hok -2 c label loops if-statement goto

#include <stdio.h>

int main()
{
    repeat:
    printf("choose an option: \n1:Draw\n2:Even or Odd:\n3:Text type\n4:How to Dec\n5:Base to Dec:\n6:Count to Bits\n0:Exit\n"); 
    int x;
    scanf("%d", &x); 
    if (x > 6)
       printf("Wrong option!\n");
       goto repeat;
    if (x == 1) 
       printf("Drawing");
    if (x == 0)
       printf("we are out!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用在线编译器。

我想看到的最终结果是一个简单的菜单,它要求用户输入数字并根据数字执行代码,然后转到起点并再次打印菜单。(这就是我选择 goto 的原因。如果您有其他解决方案可以在执行后重新启动主程序,请告诉我。)

我遇到的问题是,无论我输入多少数字,它都会再次输出菜单而没有消息或不退出(当我输入0时)。

klu*_*utt 6

C 不依赖于缩进。只是改变

if (x > 6)
       printf("Wrong option!\n");
       goto repeat;
Run Code Online (Sandbox Code Playgroud)

if (x > 6) {
       printf("Wrong option!\n");
       goto repeat;
}
Run Code Online (Sandbox Code Playgroud)

您只能省略单个语句的大括号。

但我强烈建议不要这样使用gotogoto有它的用途,但这不是其中之一。你可以像这样重写你的代码:

int main()
{
    while(1) {
        printf("choose an option: \n1:Draw\n2:Even or Odd:\n3:Text type\n4:How to Dec\n5:Base to Dec:\n6:Count to Bits\n0:Exit\n"); 
        int x;
        scanf("%d", &x); 
        if (x > 6) {
           printf("Wrong option!\n");
        }

        if (x == 1) {
           printf("Drawing");
           break;
        }
        if (x == 0) {
           printf("we are out!\n");
           break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这不是我写的方式。我改为使用尽可能少的更改的 while 循环。我会做这样的事情:

int x = 6;
while(x == 6) {
    printf("choose an option: \n1:Draw\n2:Even or Odd:\n3:Text type\n4:How to Dec\n5:Base to Dec:\n6:Count to Bits\n0:Exit\n"); 

    // Always check scanf for errors
    if(scanf("%d", &x) != 1) { perror("Input error"); exit(1); }

    switch(x) {
        case 1: printf("Drawing"); break;
        case 0: printf("we are out!\n"); break;
        default: printf("Wrong option!\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

只有两种情况我会考虑使用goto

  1. 跳出嵌套循环

  2. 在失败时清理函数中分配的资源

跳出嵌套循环的示例:

while(1) {
    while(1) {
        if(<condition>) goto END;
    }
}
END:
Run Code Online (Sandbox Code Playgroud)

清理示例:

void foo() {
    char *p = malloc(1);
    if(!p) goto END;
    char *q = malloc(1);
    if(!q) goto P;

    // Do stuff

    free(q);
P:
    free(p);
END:
}
Run Code Online (Sandbox Code Playgroud)

请注意,清理示例不是很好,因为您可以安全地释放 NULL 指针,该指针malloc在失败时返回。举个例子fopen更合适。但它展示了一般原则。您以相反的顺序进行清理,并在出现故障时跳转到适当的位置。

不管你做什么,永远不要用 goto 向后跳。只能向前跳跃。或者至少我从未见过会激发向后跳跃的情况。

在支持异常的语言中,您通常会在这两种情况下使用异常。第一个例子是这样的伪:

try {
    while(1) {
        while(1) {
            if(<condition>) throw MyException;
        }
    }
} catch MyException {}
Run Code Online (Sandbox Code Playgroud)