我的程序應該顯示所需形狀的名稱。我不習慣使用字符串,所以如何回顯用戶輸入(C顯示錐等)?我在猜測某種if語句,但不知道如何寫它。用字符串迴應用戶輸入
樣品:
Hello. Welcome to the shape volume computing program.
Which shape do you have? (C for cone, U for cube, Y, for cylinder P for pyramid, S for sphere, or Q to quit)
Enter shape: U
okay, cube. Please enter the length of a side: 3
okay, the length of the side = 3
the volume = 27
enter shape: C
okay, cone. Please enter the radius of the base: 2
please enter the height: 3
okay, the radius = 2 and the height = 3
the volume = 12.56
enter shape: Q
bye!
代碼:
int main()
{
string C,U,Y,P,S;
C= "cone";
U= "cube";
Y= "cylinder";
P= "pyramid";
S= "sphere";
int Q= -1;
cout<< " Enter C for cone, U for cube, Y for cylinder, P for pyramid, S for sphere or Q
to quit. " <<endl;
cin>> C,U,Y,P,S,Q;
if(Q= -1)
cout<< " Goodbye! " <<endl;
return 0;
}
我可以告訴你,現在,你的if語句分配Q可爲-1。 – M4rc
是的,在這種情況下應該這樣做。 –
閱讀至少前幾章C++的介紹將是一個好主意:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – jogojapan