【问题标题】:Switch statements inside while loop C++在while循环C ++中切换语句
【发布时间】:2014-12-28 11:13:51
【问题描述】:

试图编写一个程序来计算一个盒子的体积、表面积或“周长”。我希望能够为每个主题输入一个字母,并在计算出该主题之后,然后为下一个主题输入一个字母。该程序还希望在键入 Q 时结束。我相信我的数学是正确的,但这个程序对我不起作用。它编译得很好,但体积根本不会计算,尽管其他两个主题会。我也得到了三行答案,当我输入第二个字母作为第二个值时,程序运行疯了。非常感谢任何和所有帮助。感谢您的宝贵时间。

include <iostream>
include <iomanip>

using namespace std;

const char SENTINEL = 'Q';//found this suggestion online to quit a program

int main ()
{

char V, A, G, inputChar;
float length, width, depth, totalV, totalA, totalG;

cout<<"Enter character V for Volume, character A for Surface Area,\n";
cout<<"character G or Girth plus Depth or Q to quit\n"<<endl;
cin>>inputChar;// pick letter
while (inputChar!=SENTINEL)// if not Q then check the if-ele statments
{
      switch (inputChar)
      case 'V':
      {
                cout<<"Enter Length, Width, and Depth for Volume\n";
                cin>>length, width, depth;
                totalV=length*width*depth;//math for volume
                cout<<"The Volume = "<<totalV<<endl;
                break;
      }
      case 'A':
      {
               cout<<"Enter Length, Width, and Depth for Surface Area\n";
               cin>>length, width, depth;
               totalA=2*length*width+2*width*depth+2*length*depth;//math for area
               cout<<"The Surface Area = "<<totalA<<endl;
               break;
      }
      case 'G':
      {
               cout<<"Enter Length, Width, and Depth for Girth\n";
               cin>>length, width, depth;
               totalG=2*(length+width)+depth;//math for girth
               cout<<"The Girth = "<<totalG<<endl;
               break;
      }          
}
      system ("pause");
      return 0;
}

【问题讨论】:

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


    【解决方案1】:

    除了 Elliot 的回答之外,我发现您的程序有一些改进,如果没有这些改进,我认为它不会编译。就像包含语句中的“#”和错误的开关块。即使输入多个值,我们也需要级联 >> 而不是逗号。

    这是一个可以编译的代码:

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    const char SENTINEL = 'Q';//found this suggestion online to quit a program
    
    int main ()
    {
    
    char V, A, G, inputChar;
    float length, width, depth, totalV, totalA, totalG;
    
    cout<<"Enter character V for Volume, character A for Surface Area,\n";
    cout<<"character G or Girth plus Depth or Q to quit\n"<<endl;
    cin>>inputChar;// pick letter
    
    while (inputChar!=SENTINEL)// if not Q then check the if-ele statments
    {
          switch (inputChar)
          {
             case 'V':
    
                    cout<<"Enter Length, Width, and Depth for Volume\n";
                    cin>>length>>width>>depth;
                    totalV=length*width*depth;//math for volume
                    cout<<"The Volume = "<<totalV<<endl;
                    break;
    
             case 'A':
    
                   cout<<"Enter Length, Width, and Depth for Surface Area\n";
                   cin>>length>>width>>depth;
                   totalA=2*length*width+2*width*depth+2*length*depth;//math for area
                   cout<<"The Surface Area = "<<totalA<<endl;
                   break;
    
             case 'G':
    
                   cout<<"Enter Length, Width, and Depth for Girth\n";
                   cin>>length>>width>>depth;
                   totalG=2*(length+width)+depth;//math for girth
                   cout<<"The Girth = "<<totalG<<endl;
                   break;
          }
    
       cout<<"Enter character V for Volume, character A for Surface Area,\n";
       cout<<"character G or Girth plus Depth or Q to quit\n"<<endl;
       cin>>inputChar;// pick letter          
    }
          return 0;
    }
    

    【讨论】:

      【解决方案2】:

      while ( cin >> inputChar && inputChar != SENTINEL )
      {
      //... 
      

      而不是

      cin>>inputChar;// pick letter
      while (inputChar!=SENTINEL)// 
      

      你也可以写

      auto prompt = []
      {
          cout << "\nEnter character V for Volume, character A for Surface Area,\n";
          cout << "character G or Girth plus Depth or Q to quit\n"<<endl;
      };
      
      while ( prompt(), cin >> inputChar && inputChar != SENTINEL )
      {
      //... 
      

      或者

      auto prompt = []
      {
          cout << "\nEnter character V for Volume, character A for Surface Area,\n";
          cout << "character G or Girth plus Depth or Q to quit\n"<<endl;
      };
      
      prompt();
      while ( cin >> inputChar && inputChar != SENTINEL )
      {
          switch (inputChar)
          {
          case 'V':
          {
          //...
          }
      
          //...
          }
      
          prompt();
      }
      

      【讨论】:

        【解决方案3】:

        您永远不会更新您的inputChar。在您的while 循环结束时读取另一个char

          cin>>inputChar;
        }
        system ("pause");
        

        否则,当 char 与您的 switch case 或 sentinel 不匹配时,它将进入紧密循环(可能 CPU 使用率较高)。

        【讨论】:

          【解决方案4】:

          您需要在 while 循环内再次提示用户输入数据:

          do
          {
              cout << "Enter character 'V' for Volume, character 'A' for Surface Area,\n";
              cout << "character 'G' for Girth plus Depth or 'Q' to quit\n"<<endl;
              //Right here before the switch statement
              cin >> inputChar;
              switch (inputChar)
              {
                  //I also recommend stacking your cases, for lower case and upper case support:
                  case 'V':
                  case 'v':
                  {
                      //
                  }
                  case 'A':
                  case 'a':
                  {
                      //
                  }
                  case 'G':
                  case 'g':
                  {
                      //
                  }
                  //Add a default case if they're not any of the above letters
                  default:
                  {
                      cout << inputChar << " is not valid input!";
                  }
              }
          } while (inputChar != SENTINEL);
          

          【讨论】:

            【解决方案5】:

            只需添加 cin>>inputChar; 在 while 循环结束时。而已。 之后编译并运行。 您将获得预期的输出。 :-)

            -贾耶什

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-10-25
              • 1970-01-01
              • 2016-05-17
              • 1970-01-01
              相关资源
              最近更新 更多