C/C++ 如何运行相同的 switch case 直到它的条件为真并继续而不会崩溃?

标签 c console

在 C 控制台窗口中,我无法让这个应用程序检查每个检查是否一致,所以就像在 java 中那样而不是 if "while"然后它会改变,只要条件不匹配它就会一遍又一遍地循环直到你中断出

我如何在 C 语言中做到这一点?

 case 1: //add
        printf(OPENWINDOW);
        printf("\t\tAdd A Reminder  (enter the number)\n\n\n\n");
        printf("for which day?   \n\n");
        int tempday;
        scanf("%i", &tempday);
        if ( tempday >= 32){
            printf("sorry we dont go that high on days.. try again!\n");
            scanf("%i", &tempday);}
        printf("and which month my king?   \n\n");
        int tempmonth;
        scanf("%i", &tempmonth);
        if ( tempmonth > 12 ) {
            printf("sorry we dont go that high on month... try again!\n");
            scanf("%i", &tempmonth);}
        printf("what year?\n\n");
        int tempyear;
        scanf("%i", &tempyear);
        while ( tempyear < 2012 || tempyear > 2020 ){
            printf("Wow that is an incorrect year and you know it, try again\n");
            scanf("%i", tempyear);
            printf("What do you want to call this reminder?");
//            scanf("%c", char titletemp[]);

            }

        break;
        case 2: // view
        printf(OPENWINDOW);

最佳答案

While 在 C 中的工作方式与在 Java 中一样。你用 while 包裹了一段代码,它里面的任何东西都会运行,直到条件为假。

但是,您的scanf 是错误的:

scanf("%i", tempyear);

应该是:

scanf("%i", &tempyear);
            ^

注意&,意思是“传递这个变量的地址”。

关于C/C++ 如何运行相同的 switch case 直到它的条件为真并继续而不会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012632/

相关文章:

python - 将 Cython 中的 numpy 数组传递给需要动态分配数组的 C 函数

javascript - Array.prototype.concat() 底层

c - 多边形算法中的点

c - 为什么这个例子在C中陷入了无限循环?

c# - C# 中的虚数

python - 安装后无法运行 Radian(改进的 R 控制台)

firefox - Firefox 控制台中的 TypeScript 源映射?

Python同时检查来自控制台和串行的输入?

c - 无效的转义序列在 C 中如何表现?

c++ - 将源文件包含在另一个源中