【问题标题】:C++ while switch case make a loop inside defaultC ++ while switch case在默认值内创建一个循环
【发布时间】:2020-11-07 16:50:39
【问题描述】:

我在使用 while switch case 时遇到问题,我有这样的代码 我想看看用户写的不是 1,2,3,4,5,然后我想只打印“请输入选项”并要求输入新的输入,这意味着我没有想回到选择菜单,我知道它看起来很简单,我尝试使用 bool 并在 while 内部使用,但我陷入了无限循环。

   cout << "Welcome to program." << endl;
  while (true) 
    
      cout << "Please make a choice from the following menu: " << endl;
      cout << " Press 1 for this.... ," << endl;
      cout << "Press 2 for this.... . ," << endl;
      cout << "Press 3 for this.... . ," << endl;
      cout << "Press 4 for this.... . ," << endl;
      cout << "Press 5 for this.... ." << endl;
      cout << endl;
      cin >> num;
      cout << endl;
        switch (num)
     {
      case 1: 
        
          cout << "Success!" << endl;
          break;
      case 2 : 
          cout << "You presed 2 " << endl;
          break;

      case 3 : 
          cout << "You pressed 3 " ;
          break;

      case 4: 
          cout << "You pressed 4 " ;
          cout << endl;
          break;
      case 5 : 
           cout << "You pressed 5 " ;
          
         
      default:
             cout << "Please enter a valid option!" << endl; // only print this while user write other than cases
             cin >> num;
           
             cout << endl;

【问题讨论】:

  • 你永远不会脱离while循环,所以你会陷入while循环
  • 提问时请使用标点符号。
  • @Abhishek 实际上在我按 5 时返回 -1 并退出程序,但我找不到在默认值中创建循环的内容,并在输入无效时打印输入有效选项。
  • 那么您可能在问题描述中添加了一些不同的内容。即使我输入 1-5 个选项,我也尝试了代码并无限运行
  • 完全跑题了,但它真的让我很烦:你使用的是字符 ı,它是“LATIN SMALL LETTER DOTLESS I”(unicode C4 B1)。为什么?什么样的情况最终导致你写这个而不是I,特别是因为我看到你也同时使用iI。只是好奇。

标签: c++ while-loop switch-statement boolean


【解决方案1】:
while (true) 
  

我可以看到您在 while 循环之后没有使用括号,所以您希望它如何正确循环。如果没有括号,它只会执行下一行代码。所以,把你的代码封装在while(true)

除此之外,您永远不会脱离 while 循环,因此您会被困在 while 循环中。您在 while 循环中没有任何条件可以随时使您跳出循环。 switch-case 块中的breaks 只会让你脱离 switch-case 块,但你仍然会留在 while 循环中。所以,要想跳出循环,就必须在switch块外跳出循环。

您可能会维护一个标志,指示选项 1、2、3、4、5 是否执行,然后您可以跳出循环。或者您可以考虑适合您的用例的逻辑。我只是指出了你哪里出错了。

正确代码 -

cout << "Welcome to program." << endl;
int num=3;
while (true) {
  if(num>=1&&num<=5){
  cout << "Please make a choice from the following menu: " << endl;
  cout << " Press 1 for this.... ," << endl;
  cout << "Press 2 for this.... . ," << endl;
  cout << "Press 3 for this.... . ," << endl;
  cout << "Press 4 for this.... . ," << endl;
  cout << "Press 5 for this.... ." << endl;
  cout << endl;
    }
  cin >> num;
  cout << endl;
    switch (num)
 {
  case 1:         
      cout << "Success!" << endl;
      break;
  case 2 : 
      cout << "You presed 2 " << endl;
      break;

  case 3 : 
      cout << "You pressed 3 " ;
      break;

  case 4: 
      cout << "You pressed 4 " ;
      cout << endl;
      break;
  case 5 : 
       cout << "You pressed 5 " ; 
       break;                  
  default:
         cout << "Please enter a valid option!" << endl; // only print this while user write other than cases
        continue;
     }
}

上面的代码会一直循环,直到用户输入了一个无效的选项,并且只有在用户输入了一个正确的选项后才退出循环。

【讨论】:

  • 实际上我有括号,但我不能在 stakcoverflow 中写,这是我的错,我没有寻找从程序中突破的方法,而是找到一种在默认值和如果用户输入 1 或 2 等,程序流程将继续进行。我试图在默认情况下设置一个标志,但它会导致无限循环
  • @Razbolt 为什么要在已经存在的循环中创建另一个循环。如果用户在默认语句中写入continue; 输入无效选项,您可以继续循环
  • @Razbolt 即使用户在默认循环中输入了一个有效选项,您也会在 while 循环中从头开始循环并再次询问用户输入。
  • 它继续进入 while(true) 循环,该循环打印出程序菜单,但是我不想打印出程序菜单,而我在默认值中并且程序一直运行到用户输入 5 它实际上返回 -1,
  • 继续;继续进入while循环,但我的意思是再次默认而不是整个程序
【解决方案2】:

任何你不想重复的东西,不要循环。就是这么简单。在循环外显示完整菜单和第一个提示。您的默认案例会在输入错误号码时打印其消息。我还缩小了开关,因为所有额外的输入都是不必要的。 [[fallthrough]] 是一个很好的工具,但它需要 C++17。

如果你想要的只是一个有效的输入,你真的不应该为while (true) 无限循环而烦恼。只要您的输入无效,就进行迭代。

#include <iostream>

int main() {
  int num = 0;

  std::cout << "Welcome to the program.\n";
  std::cout << "Please make a choice:\n"
            << "1.\n2.\n3.\n4.\n5.\n\n"
            << "Please enter option: ";

  while (num < 1 || num > 5) {
    std::cin >> num;

    switch (num) {
      case 1:
        std::cout << "Success!\n";
        break;
      case 2:
        [[fallthrough]];  // C++17 needed for this, otherwise leave empty
      case 3:
        [[fallthrough]];
      case 4:
        [[fallthrough]];
      case 5:
        std::cout << "You pressed " << num << ".\n";
        break;
      default:
        std::cerr << "Invalid entry!\nPlease enter a **valid** option: ";
    }
  }
}

【讨论】:

  • OP 希望程序在每次用户输入正确的输入时打印菜单。我不认为你的程序这样做
  • 它没有,我不认为 OP 想要那样。
  • 当用户输入错误的输入时,OP 只不想打印菜单(在这种情况下,OP 想要打印 Invalid entry!...您的程序确实如此),但在其他情况下,他只是希望循环继续打印菜单
  • 我看到了那个评论,它比你的要新得多。我将按原样保留我的代码。我的理由是这个请求是零意义的,而且他们的英语很糟糕,我无法准确说出他们想要什么。也许如果他们正确地提出了这个问题,比如概述一些示例输入和预期输出与接收到的输出,我们就不必猜测和阅读整个页面上的所有 cmets 来了解所需的行为应该是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 2021-10-01
  • 2015-07-10
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多