我做了一个switch case程序,当我给出输入时它不会执行switch case

时间:2018-01-11 08:58:04

标签: c switch-statement

void booking() {           //globally declared function
    printf("please select the seats");
    printf("1A 2A 3A G");
}

int main() {                //Main function
    int n;
    clrscr();
    printf("\t\t\t Railway reservation system");
    printf("1.Booking");
    printf("2.Availability checking");
    printf("3.Cancellation");
    printf("4.Prpare chart");
    scanf("%d",&n);

    switch(n)  //when I give input as 1 switch case is not being executed
    {
    case 1:
        booking();  //after the input it should execute this global function.
        break;

    case 2:  
        break;

    case 3:
        break;

    case 4:
        break;
    }

    return n;
}

我的目标是当我们给出输入时它应该执行全局声明的函数。在输入之后它没有进入switch语句。

2 个答案:

答案 0 :(得分:0)

实际上已经执行了switch-case,但是当你的程序快速完成时,你可能无法看到它。尝试从命令行执行它或在最后添加这样的东西:

do {
    printf("Press q to quit: ");
    user_in = getchar();
} while (user_in != 'q');

还有一件事:在菜单的每一行末尾添加换行符(' \ n')。

答案 1 :(得分:-1)

放置

printf("Press any key to exit...");
while (!kbhit()) ;

return语句之前,您会在关闭之前看到控制台屏幕。

相关问题