在循环C++时切换内部语句
Switch statements inside while loop C++
试图编写一个程序来计算长方体的体积、表面积或"周长"。我希望能够为每个主题输入一个字母,在它计算出该主题后,再为下一个主题输入一封字母。程序还希望在键入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
相关文章:
- 将函数类成员映射到类本身内部
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- Boost Spirit,获取迭代器内部语义动作
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 内联函数中具有内部链接的全局变量
- 有人可以解释一下吗?这是关于数组和数组内部 if 语句的一些处理
- C++ While(1) 循环内部没有调用 if 语句
- 如果语句内部循环不读取完全正确的代表显示错误
- 我如何为内部列表提供基于范围的语句?(C )
- 不带内部作用域的 if 语句
- 在for语句参数内部的C/C++算术运算中
- 是 for() 循环维护在堆栈内部或 for 只是一个语句
- 在循环C++时切换内部语句
- 循环内部的赋值语句
- 工厂内部If语句过多
- 如何使用 break 语句停止内部和外部循环
- Switch case语句如何在内部实现或工作