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");
}
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.
#include <iostream>
#include <string>
usingnamespace 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;
}elseif (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;
}
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!