It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
7年前关闭。
我的程序应该显示所请求形状的名称。我不习惯使用字符串,因此如何回显用户输入(C显示圆锥体等)?我在猜测某种if语句,但不确定如何编写。
样品:
码:
手段
因为逗号运算符的优先级最低。
因此,它将一个字符输入到
那可能不是您想的那样。
为了使这项工作可以
使用单个输入变量,例如
使用
检查输入的行是
7年前关闭。
我的程序应该显示所请求形状的名称。我不习惯使用字符串,因此如何回显用户输入(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;
}
最佳答案
该声明
cin>> C,U,Y,P,S,Q;
手段
(cin>> C),U,Y,P,S,Q;
因为逗号运算符的优先级最低。
因此,它将一个字符输入到
C
,然后(这是逗号运算符的作用)对U
,Y
,P
,S
和Q
求值,后者的值作为表达式结果,然后将其丢弃。那可能不是您想的那样。
为了使这项工作可以
使用单个输入变量,例如
line
。使用
getline
标头中的<string>
函数输入一行。检查输入的行是
"U"
,在这种情况下执行U还是在其他情况下执行其他操作。 if
语句对此很有用。关于c++ - 用字符串回显用户输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13079994/