在c ++中为字符串变量赋值

时间:2017-10-26 05:53:16

标签: c++ string

这段代码是大型程序的一部分,我还是新编码,所以我可能错了,但是当我执行时,不应该在半径和高度之后在屏幕上显示形状名称?即使我为方形输入2,它目前也会显示三角形。

cout << "Please enter your choice of shape from the following menu: \n\n"
        << "1. triangle\n"
        << "2. square\n"
    cin >> choice;    

if (choice = 1)
    shape = "triangle" ;
else if (choice = 2)
    shape = "square";

cout << "Enter the side of the " << shape << ": ? ";
cin >> side;

1 个答案:

答案 0 :(得分:1)

choice==1否则你要分配它。你应该使用相等性检查。

if (choice == 1)
    shape = "triangle" ;
else if (choice == 2)
    shape = "square";