My else statement won't display. Any suggestions?

My "if" statement displays but my "else" statement does not. Any ideas how I can fix this? (this is my only error)

#include <iostream>
#include <string>
using namespace std;

int main()
{
int sum_angle;
int num_sides;
int r_d; // radians/degrees
char R; //input radians
float radian;

cout << "Basic Geometry with c++";
cout << "\n-----------------------\n";
cout << "Enter the number of sides of your polygon (3-10) :\n";
cin >> num_sides;
cout << "display options:\n";
cout << " - Type R for radians\n";
cout << " - Type anything else for degrees\n";
cin >> R;
sum_angle=(((num_sides)-2)*180);
radian=(sum_angle*(3.14/180));


if (R)
{

cout << "The sum of interior angles in your polygon is:" << radian << "radians";
cout << "\nA polygon with " << num_sides << " sides is called a ";
}

else
{
sum_angle=(((num_sides)-2)*180);

cout << "The sum of interior angles is:"<< sum_angle;
cout << "\nA polygon with " << num_sides << " sides is called a ";

}




if (num_sides==3)
cout << "triangle";

if (num_sides==4)
cout << "quadrilateral or tetragon";

if (num_sides==5)
cout << "pentagon";


return 0;
}
closed account (o3hC5Di1)
Hi there,

your statement if (R) will always be true, so the else block will never be evaluated.
What you mean is actually: if (R=='r' || R=='R').

Hope that helps.

All the best,
NwN
Last edited on
if (R) is testing the boolean (true or false) value of the variable R. If R is zero, then the result is false, anything else will be true.

Since the ASCII code of any character you type on the keyboard will not be zero, the result will always be true.

I think you meant
if (R == 'R')
or to include upper or lower case responses,
if (R == 'R' || R == 'r')
Oops @NwN, you accidentally put '=' instead of '=='.
closed account (o3hC5Di1)
Ah yes, my apologies, thanks for pointing that out.

All the best,
NwN

Thank you so much it works now. I didn't expect such a quick response. I am very impressed and glad that I just became a member of this forum. Thanks again.
Topic archived. No new replies allowed.