qq_41933131 2021-10-11 22:04 采纳率: 85.7%
浏览 22
已结题

为什么goto循环不能继续下去?

按照我的设想,应该重回loop位置后再有scanf请求输入的,然后到switch继续循环,但是运行结果确实直接跳过去了,为什么?


#include<stdio.h>
#include<string.h>

typedef struct {
    int sit_num;
    int order;
    char nachname[20];
    char vorname[20];
}plane;
 
void plan_a(plane *p);
void plan_b(plane *p);
void plan_c(plane *p);
void plan_d(plane *p);
void plan_e(plane *p);



int main() {
    plane A[11]={};//运行前自行初始化录入数据 
    



    loop: printf("To choose a functions, enter its letter label:\n");
    printf("a)    Show number of empty seats\n");
    printf("b)    Show list of empty seats\n");
    printf("c)    Show alphabetical list of seats\n");
    printf("d)    Assign a customer to a seat assignment\n");
    printf("e)    Delete a seat assignment\n");
    printf("f)    Quit\n");
    char n, m;
    scanf("%c", &n);
    if (n == 'f')
        goto q2;
    switch (n)
    {
    case 'a':plan_a(A); goto loop; break;
    case 'b':plan_b(A); goto loop; break;
    case 'c':plan_c(A); goto loop; break;
    case 'd':plan_d(A); goto loop; break;
    case 'e':plan_e(A); goto loop; break;
    case 'f':break;

    default:
        break;
    }


q2:printf("程序结束");
    return 0;
}


void plan_a(plane *p) {
    int LL = 0;
    
    printf("如要中止,请按1后回车;如需继续,请按0后回车");
    scanf("%d", &LL);
    if (LL == 1)
        goto qq;


    int sum_order = 0;
    int i=0;
    for (i = 0; i < 12; i++) {
        if ((p+i)->order == 0)
            sum_order = sum_order + 1;
    }
    printf("%d\n", sum_order);


qq:if (LL == 1)
    printf("返回菜单");
}
void plan_b(plane *p) {
    int LL = 0;

    printf("如要中止,请按1后回车;如需继续,请按0后回车");
    scanf("%d", &LL);
    int i=0;
    if (LL == 1){
    
        goto qq;}


    for (i = 0; i < 12; i++)
    {
        if ((p+i)->order == 0)
            printf("%d\n", (p+i)->sit_num);
    }


qq:if (LL == 1)
    printf("返回菜单");
}
void plan_c(plane *p) {
    int LL = 0;
    printf("如要中止,请按1后回车;如需继续,请按0后回车");
    scanf("%d", &LL);
    if (LL == 1)
        goto qq;

    int i=0;
    for (i = 0; i < 12; i++)
    {
        printf("%d\n", (p + i)->sit_num);
    }

qq:if (LL == 1)
    printf("返回菜单");
}
void plan_d(plane *p) {
    int LL = 0;
    printf("如要中止,请按1后回车;如需继续,请按0后回车");
    scanf("%d", &LL);
    if (LL == 1)
        goto qq;

    int L2 = 0;
        printf("请输入预订座位号\n");
        scanf("%d", &L2);
        (p + L2)->order = 1;
        printf("请输入预订人的名\n");
        scanf("%s", &(p + L2)->nachname);
        printf("请输入预定人的姓\n");
        scanf("%s", &(p + L2)->vorname);

    qq:if (LL == 1)
        printf("返回菜单");
}
void plan_e(plane *p) {
    int LL = 0;
    printf("如要中止,请按1后回车;如需继续,请按0后回车");
    scanf("%d", &LL);
    if (LL == 1)
        goto qq;


    int L3 = 0;
    printf("请输入删除座位编号");
    scanf("%d", &L3);
    char zero[20] = { 0 };
    strcpy((p + L3)->nachname, zero);
    strcpy((p + L3)->vorname, zero);
    (p + L3)->order = 0;
qq:if (LL == 1)
    printf("返回菜单");

}

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-10-11 22:36
    关注

    主要是你每个plane_a到plane_e函数中都有scanf输入。输入数据后你按了回车键,导致goto loop后,scanf("%c",&n)会自动接收这个换行符,从而switch的时候执行了default,break掉了
    你在plane_a到plane_e的scanf语句后,调用getchar();消化掉换行符就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月19日
  • 已采纳回答 10月11日
  • 创建了问题 10月11日

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?