简体   繁体   English

编程新手-C-菜单系统掉线问题

[英]Programming Novice - C - Issue with Fall-through in Menu System

I have recently begun studying C just for the fun of it and I decided to have a go at creating a menu system, but I'm having an issue with what I think is fall-through: 我最近刚开始学习C只是为了好玩,所以我决定尝试创建菜单系统,但是对于我认为存在的问题,我遇到了一个问题:

#include <stdio.h>

int main (void)
{

int x, y, z;

start:
printf ("Menu 1 -- Please select a number.\n");
printf ("1\n");
printf ("2\n");
printf ("Enter selection: ");
scanf ("%i", &x);
getchar();

if (x == 1) {
    x_one:
    printf ("1.\n");
    printf ("Menu 2 -- Please select a number.\n");
    printf ("1\n");
    printf ("2\n");
    printf ("3 -- Go back.\n");
    printf ("Enter selection: ");
    scanf ("%i", &y);
    getchar();

    if (y == 1) {
        y_one_one:
        printf ("1-1.\n");
        printf ("Menu 3 -- Please select a number.\n");
        printf ("1\n");
        printf ("2\n");
        printf ("3 -- Go back.\n");
        printf ("Enter selection: ");
        scanf ("%i", &z);
        getchar();

        if (z == 1 || z == 2)
            printf ("You selected %i.\n", z);

        if (z == 3)
            goto x_one;

        else if (z > 1 && z < 3)
            goto y_one_one;
    }

    if (y == 2) {
        /* mostly the same as "if (y == 1)". */
    }

    if (y == 3)
        goto start;

    else {
        printf ("Please enter a valid selection.\n");
        goto x_one;
    }
}

if (x == 2) {
    /* Mostly the same as "if (x == 1)". */
}

else if (z > 1 && z < 2)
    goto start;

return 0;
}

The issue is, once I have reached the third menu, the program will print out the string of the selected number, but instead of terminating itself it will either display the third menu or go back to the second menu, and it will do this regardless of what number I input. 问题是,一旦我到达第三个菜单,程序将打印出所选数字的字符串,但不是终止自身,而是显示第三个菜单或返回第二个菜单,并且无论我输入的数字。

I've only been studying C for about two weeks and I don't quite have a proper understanding on how nested if statements work, if I could please get an explanation of what's causing this unexpected behavior and how to correct it that would be greatly appreciated, thank you. 我只是一直在研究下,在约两个星期,我不太有关于如何嵌套一个正确的认识if语句的工作,如果我能请得到的是什么导致了这个意外的行为的解释以及如何纠正它,这将是很大的谢谢,谢谢。

I am using GCC 5.1.0 through TDM-GCC-64 on Windows 10. 我正在Windows 10上通过TDM-GCC-64使用GCC 5.1.0。

if (y == 3)
    goto start;

else {
    printf ("Please enter a valid selection.\n");
    goto x_one;

Thats where its looping back up. 多数民众赞成在其循环备份。 If y is equal to 3 you go the start, if y is not equal to 3 then go back to start. 如果y等于3,则重新开始;如果y不等于3,则重新开始。

So basically its saying go back to beginning no matter what. 所以基本上不管怎么说,它的说法可以追溯到开始。

Remember this executes even after you enter Z. Code keeps going down unless you redirect it. 请记住,即使您输入Z,它也会执行。除非您重定向它,否则代码会不断下降。

You'll probably get comments on the form of your programming, imo its fine for a newb, you'll learn better with time and/or after getting burned. 您可能会收到有关程序形式的注释,如果您不喜欢它,那将是一件好事,随着时间的流逝和/或在被烧毁之后,您会学得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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