I think I may be using if wrong and it is messing up my code...

I think I may be using if wrong, but I can't figure out another way to do it. I'm trying to make it so if the user types in rectangle, it brings up a certain thing and if they type circle it does something else.

#include <iostream>
#include <string>

using namespace std;

int main()

{
string myname;
string shape;
float length;
float width;
float radius;
float area;
float arearec;
cout << " Please enter your name: \n" <<endl;
cin >> myname;
cout << "\n Enter the shape you would like the area of (Rectangle=1 or Circle=2): \n" <<endl;
cin >> shape;
if (shape=1) {
cout << "\n Enter the length of the rectangle: \n" <<endl;
cin >> length;
cout << "\n Enter the width of the rectangle: \n" <<endl;
cin >> width;
arearec=length*width;
cout << "\n\n Hello " << myname <<". " << "The area of your rectangle is " <<arearec << "." <<endl;
system("pause");
}
if (shape=2) {
cout << "\n Please enter the radius of your circle: \n" <<endl;
cin >> radius;
cout << "\n The radius is " << radius <<"." <<endl;
area=radius*radius*3.14;
cout << "\n The area is " << area <<"." <<endl;
cout << "\n\n Hello " << myname <<". " << "The area of your circle is " <<area << "." <<endl;
system("pause");
}
Last edited on
Use code tags please.

if (shape=1)

= is the assignment operator, == is the conditional (conditional?) operator you are looking for.

if (a==b)

shape should also be an integer value, not a string.
Is there a way to make shape an integer value? And what do you mean code tags?
A very common mistake:

Using if/else statement:
if(A==B) Please note your synthx!

This is wrong:
if(shape=1)

Instead:
if(shape==1)

Secondly, the "system("pause") should not be within the if/else statement and you did not return anything from your main function after the execution is completed.

Thirdly, since you're using string for "shape".
Your if else statement should look like this:

if(shape=="1")
instead of
if(shape == 1)
The reason is because the second one simply means that your shape is an integer istead of string value.

Hence, your code should look like:
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
 #include <iostream>
#include <string>

using namespace std;

int main()
{
string myname, shape;
float length, width, radius, area, arearec;
cout << " Please enter your name: \n" <<endl;
cin >> myname;
cout << "\n Enter the shape you would like the area of (Rectangle=1 or Circle=2): \n" <<endl;
cin >> shape;
if (shape=="1") {
cout << "\n Enter the length of the rectangle: \n" <<endl;
cin >> length;
cout << "\n Enter the width of the rectangle: \n" <<endl;
cin >> width;
arearec=length*width;
cout << "\n\n Hello " << myname <<". " << "The area of your rectangle is " <<arearec << "." <<endl;
}else if (shape=="2") {
cout << "\n Please enter the radius of your circle: \n" <<endl;
cin >> radius;
cout << "\n The radius is " << radius <<"." <<endl;
area=radius*radius*3.14;
cout << "\n The area is " << area <<"." <<endl;
cout << "\n\n Hello " << myname <<". " << "The area of your circle is " <<area << "." <<endl;
}

system("pause");
return 1;
}


=)
Oh I see. Thanks! I appreciate it.
Is there a way to make shape an integer value? And what do you mean code tags?


You use:

string shape;

Instead use:

int shape;

@lwtan90

Why return 1;?
Ya that worked lwtan90. Now I need to work on adding other shapes.
Danny:

return 1;

This is just a safety precaution to return "something" that you use during the compilation and execution of this program back to your machine/computer!
Topic archived. No new replies allowed.