Can anyone answer why both if statement run?

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
float area;
float triangle;
float base;
float height;
float side;
char letter;




cout << "Enter in which type you would like:";
cin >> letter; //get a charector ('t' or 's')
cout << endl;



if (letter = 't');
{

cout << "The base: ";
cin >> base; // get the base
cout << endl;

cout << "Enter in the height: ";
cin >> height; // get the height
cout << endl;

triangle = (base*height) / 2;// calculate the triangle area
cout << "The triangle area is : " << triangle; // disp;ay the triangle area
cout << endl;
}


if (letter == 's')
{

cout << "Enter in the side: ";
cin >> side; // get the side of the square
cout << endl;

area = pow(side, 2); // compute the area of a square
cout << "The area is :" << area; // display the area
cout << endl;
}


return 0;

}
Last edited on
Hey. Please edit your post and use code tags for all of your code, to make it more readable - http://www.cplusplus.com/articles/jEywvCM9/

One if statement is perfectly fine, but somehow you managed to very much mess up the other one :p

if (letter = 't'); // should be == not =, = is an assignment operator, == is equal to operator. Also remove the semicolon
Last edited on
the == error is common, try to remember it next time!
Thank you for the "==" suggestion. I just missed it when i was reading my code
There are two ways to avoid this problem.

1. Listen to your compiler.
warning C4390: ';' : empty controlled statement found; is this the intent?

2. write
1
2
if ('t' == letter)
if you try if ('t' = letter) it won't compile 
Topic archived. No new replies allowed.