Jac*_*awk 3 c++ while-loop switch-statement
试图编写一个程序来计算盒子的体积,表面积或"周长".我希望能够为每个主题输入一个字母,然后在计算该主题后,输入下一个主题的字母.该程序还希望在键入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;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
除了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 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;
}
Run Code Online (Sandbox Code Playgroud)
写
while ( cin >> inputChar && inputChar != SENTINEL )
{
//...
Run Code Online (Sandbox Code Playgroud)
代替
cin>>inputChar;// pick letter
while (inputChar!=SENTINEL)//
Run Code Online (Sandbox Code Playgroud)
你也可以写
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 )
{
//...
Run Code Online (Sandbox Code Playgroud)
要么
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5680 次 |
| 最近记录: |