用户输入以确定主函数是否重新启动或结束

发布于 2025-01-14 16:24:59 字数 1091 浏览 2 评论 0原文

我现在正在开发一个小项目,该项目允许用户从 switch 语句中选择数组编辑功能。不幸的是,我的编码很糟糕(此时我无法使开关菜单成为其自己的功能,它是 main() 的一部分。)我提出这一点只是为了防止涉及调用菜单函数的建议。我也无法使用 main() 递归,因为我接受数组的用户输入并且不想替换现有值。

下面的代码应该接受用户输入来决定是否退出程序或重新打开选择屏幕

char restart = 'y';
    
      do {
    
        printf("Would you like to try a different function? (Y/N): ");
        restart = getchar();
        restart = getchar();
    
        system("clear");
        goto START; // "START" is the label for my main selection screen function.
        
      } while ( restart == 'Y' || restart == 'y');
      
      system("clear");
      printf("Okay, thank you!\n\n");
      
      for (i = 3; i > 0; i++) {
        printf("Exiting in %d seconds...", &i);
        sleep(1);
      }

(在 main() 的末尾我包含了一个退出函数。)

我有两个问题:

  1. 如何我检测用户是否输入了无效字符? (不是 n、N、y、Y)我最初打算使用 if-else 语句来构建它,但意识到这是无效的。但是,现在我不再知道如何使用 do-while 循环来检测这些输入。

  2. 这段代码根本不起作用。我做错了什么?当输入 y 或 Y 时,循环永远不会退出。我应该利用中断将 do-while 循环嵌入到 while 循环中吗?

我现在唯一的理论(作为一个初级 C 程序员)是标签不适用于此方法,或者我没有真正的方法来打破循环。

谢谢你!

I'm working on a small project right now that allows a user to choose an array editing function from a switch statement. Unfortunately, I coded it poorly (I can't make the switch menu its own function at this point, it is part of main().) I only bring this up to prevent suggestions involving calling a menu function. I also cannot use main() recursion because I take user input for the array and do not want to replace the existing values.

The code below is supposed to take user input to decide whether to exit the program or to re-open the selection screen and

char restart = 'y';
    
      do {
    
        printf("Would you like to try a different function? (Y/N): ");
        restart = getchar();
        restart = getchar();
    
        system("clear");
        goto START; // "START" is the label for my main selection screen function.
        
      } while ( restart == 'Y' || restart == 'y');
      
      system("clear");
      printf("Okay, thank you!\n\n");
      
      for (i = 3; i > 0; i++) {
        printf("Exiting in %d seconds...", &i);
        sleep(1);
      }

(At the end of main() I included an exit function.)

I have two questions:

  1. How can I detect if the user inputs an invalid character? (Not n, N, y, Y) I was initially going to build this with if-else statements, but realized that was ineffective. However, now I no longer know how to work with the do-while loop to detect these inputs.

  2. This code simply doesn't work. What'd I do wrong? When inputting y or Y, the loop never exits. Should I embed the do-while loop in a while loop utilizing breaks?

My only theories right now (as a baby C programmer) are that labels don't work with this method or that I have no real way to break from the loop.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文