在循环C++时切换内部语句

Switch statements inside while loop C++

本文关键字:内部 语句 循环 C++      更新时间:2023-10-16

试图编写一个程序来计算长方体的体积、表面积或"周长"。我希望能够为每个主题输入一个字母,在它计算出该主题后,再为下一个主题输入一封字母。程序还希望在键入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 quitn"<<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 Volumen";
                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 Arean";
               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 Girthn";
               cin>>length, width, depth;
               totalG=2*(length+width)+depth;//math for girth
               cout<<"The Girth = "<<totalG<<endl;
               break;
      }          
}
      system ("pause");
      return 0;
}

添加到Elliot的答案中,我发现您的程序中有一些改进,如果没有这些改进,我认为它不会编译。类似include语句中的"#"和错误的开关块。即使输入多个值,我们也需要级联>>,但不需要逗号。

下面是一个可以编译的代码:

#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 quitn"<<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 Volumen";
                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 Arean";
               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 Girthn";
               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 quitn"<<endl;
   cin>>inputChar;// pick letter          
}
      return 0;
}

写入

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 quitn"<<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 quitn"<<endl;
};
prompt();
while ( cin >> inputChar && inputChar != SENTINEL )
{
    switch (inputChar)
    {
    case 'V':
    {
    //...
    }
    //...
    }
    prompt();
}

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

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

否则,当char与您的开关情况或sentinel不匹配时,它将进入一个紧密的循环(可能是高cpu使用率)。

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

do
{
    cout << "Enter character 'V' for Volume, character 'A' for Surface Area,n";
    cout << "character 'G' for Girth plus Depth or 'Q' to quitn"<<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);

只需添加cin>>inputChar;在while循环结束时。就是这样。然后编译&跑您将获得预期的输出。:-)

-Jayesh