試圖編寫一個程序來計算盒子的體積,表面積或「周長」。我希望能夠爲每個主題輸入一個字母,然後在計算該主題後,爲下一個主題輸入一個字母。該程序還希望在鍵入Q時結束。我相信我的數學是正確的,但這個計劃對我來說並不合適。它編譯得很好,但卷不會計算,但其他兩個主題將會。我還得到了三行答案,當我輸入第二個字母作爲第二個值時,程序瘋狂。任何和所有的幫助,非常感謝。謝謝你的時間。while循環內部的開關語句C++
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;
}