error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Alloc>' is illegal

i am making a perimeter/area code that first asks which shape the user would like to find... i am pretty sure that my issue is that im using string incorrectly... could also be the "else if"s. could anyone point me in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// area and perimeter finder//
#include <iostream> 
#include <string>

using namespace std; 

string shape;
string rectangle = "a";
string circle = "b";
string triangle = "c";
string ellipse = "d";
string sector = "e";
string trapezoid = "f";
string square = "g";
double pi = 3.14159;

int main () 

{ 
cout << "what shape do you want to find the area and perimeter of?" << endl;
cout << "type a for rectangle" << endl;
cout << "type b for circle" << endl;
cout << "type c for triangle" << endl;
cout << "type d for ellipse" << endl;
cout << "type e for sector" << endl;
cout << "type f trapezoid" << endl;
cout << "type g for square" << endl;
cin >> shape;

if (shape = rectangle)
{
 double lengthRectangle, widthRectangle, areaRectangle, perimeterRectangle; 
 cout << "Enter the length of a rectangle: "; 
 cin >> lengthRectangle; 
 cout << "Enter the width of the rectangle: "; 
 cin >> widthRectangle; 
 cout << endl; 
 areaRectangle = lengthRectangle * widthRectangle; 
 perimeterRectangle = 2 * lengthRectangle + 2 * widthRectangle; 
 cout << "The area of the rectangle with length " << lengthRectangle; 
 cout << " units and width " << widthRectangle << " units"<< endl; 
 cout << "is " << areaRectangle << " square units. The perimeter is "; 
 cout << perimeterRectangle << " units."; 
 cout << endl << endl; 
}

else if (shape = circle)
{
double radiusCircle, areaCircle, perimeterCircle;
cout << "enter the radius of the circle";
cin >> radiusCircle;
areaCircle = pi * (radiusCircle * radiusCircle);
perimeterCircle = 2 * pi * radiusCircle;
cout << "the area of the circle with radius " << radiusCircle << "units is " << areaCircle;
cout << "the circumference is " << perimeterCircle;

}

else if (shape = triangle)
{
double baseTriangle, widthTriangle, heightTriangle, hypotenuseTriangle, areaTriangle, perimeterTriangle;
cout << "enter the base of the triangle: ";
cin >> baseTriangle;
cout << "enter the height of the triangle: ";
cin >> heightTriangle;
areaTriangle = baseTriangle * heightTriangle * .5;
cout << "the area of the triangle is " << areaTriangle << " units.";
widthTriangle = baseTriangle * .5;
hypotenuseTriangle = sqrt((widthTriangle * widthTriangle) + (heightTriangle * heightTriangle));
perimeterTriangle = (2 * hypotenuseTriangle) + widthTriangle;
cout << "the perimeter of the triangle is " << perimeterTriangle << " units.";
}

else if (shape = ellipse)
{
double heightEllipse, widthEllipse, areaEllipse, perimeterEllipse;
cout << "enter the height of the ellipse: ";
cin >> heightEllipse;
cout << "enter the width of the ellipse: ";
cin >> widthEllipse;
areaEllipse = pi * heightEllipse * widthEllipse;
cout << "the area of the ellipse is " << areaEllipse << " units.";
perimeterEllipse = 2 * pi * (sqrt(((heightEllipse * heightEllipse) + (widthEllipse * widthEllipse)) / 2));
cout << "the perimeter of the ellipse is " << perimeterEllipse << " units.";
}

else if (shape = sector)
{
double radiusSector, thetaSector, areaSector, perimeterSector;
cout << "enter the radius of the sector: ";
cin >> radiusSector;
cout << "enter the degree (in radians) of the angle of the sector: ";
cin >> thetaSector;
areaSector = .5 * (radiusSector * radiusSector) * thetaSector;
cout << "the area of the sector is " << areaSector << " units.";
perimeterSector = 2 * radiusSector + ((thetaSector * pi * radiusSector) / 180);
cout << "the perimeter of the sector is " << perimeterSector << " units.";
}

else if (shape = trapezoid)
{
double heightTrapezoid, aTrapezoid, bTrapezoid, areaTrapezoid, perimeterTrapezoid, hypotenuseTrapezoid;
cout << "enter the width of the top of the trapezoid: ";
cin >> aTrapezoid;
cout << "enter the width of the bottom of the trapezoid: ";
cin >> bTrapezoid;
cout << "enter the height of the trapezoid: ";
cin >> heightTrapezoid;
areaTrapezoid = (.5 * (aTrapezoid + bTrapezoid)) * heightTrapezoid;
cout << "the area of the trapezoid is " << areaTrapezoid << " units.";
hypotenuseTrapezoid = sqrt ((((bTrapezoid - aTrapezoid) / 2) * (bTrapezoid - aTrapezoid) / 2)) + (heightTrapezoid * heightTrapezoid);
perimeterTrapezoid = hypotenuseTrapezoid + hypotenuseTrapezoid + aTrapezoid + bTrapezoid;
cout << "the perimeter of the trapezoid is " << perimeterTrapezoid << " units.";
}

else if (shape = square)
{
double lengthSquare, areaSquare, perimeterSquare;
cout << "enter the length of one of the square's sides.";
cin >> lengthSquare;
areaSquare = lengthSquare * lengthSquare;
cout << "the area of the square is " << areaSquare << " units.";
perimeterSquare = 4 * lengthSquare;
cout << "the perimeter of the square is " << perimeterSquare << " units.";
}

 return 0; 
}
the if statement takes a condition, and what you're actually doing is assigning one string to another.
What the compiler is really trying to tell you is it can't convert the result of the assignment (which is a string) into a boolean, which is required to evaluate the if statement.

You need to use the == operator, not the = operator:
if (shape == circle)
that did the trick :D thank you. i never remember that double equal operator for some reason.
Topic archived. No new replies allowed.