If... Else in C++
Hey guys. I am having a problem with my If...Else statements.
Here is my code.
if ( x <= 1, y <=2)
InchesToFeet();
else if ( x <= 1, y <=3)
InchesToYards();
else if ( x <= 1, y <=4)
InchesToMiles();
else if ( x <= 1, y <=5)
InchesToMillimeters();
else if ( x <= 1, y <=6)
InchesToCentimeters();
else if ( x <= 1, y <=7)
InchesToMeters();
else if ( x <= 1, y <=8)
InchesToKilometers();
else if ( x <= 2, y <=1)
FeetToInches();
Now, everything works up to
else if ( x <= 2, y <=1)
FeetToInches();
When I type in 2, and 1, so that x = 2 and y = 1, it calls the function InchesToFeet rather than FeetToInches.
Now if I set them all to = and not >= then only the InchesToFeet function is called and nothing works.
Any ideas? I hope I made myself clear.
The comma operator is a sequence point. It doesn't do what you think it does.
You should be using logical operators.
You should be using logical operators.
Quote:Original post by Cadoink
Awesome. I just replaced everything with ==.
Thanks!
You've missed the point entirely. Perhaps you should actually read the contents of those links.
you should have replaced yours commas with the 'and' operator (&&), nothing to do with the == operator.
read the given link, your code should look like:
if (x <= 1 && y <= 2)
...
else if (x <=1 && y <=3)
...
read the given link, your code should look like:
if (x <= 1 && y <= 2)
...
else if (x <=1 && y <=3)
...
I would try something like:
enum units {INCHES = 1, FEET, YARDS, MILES,
MILLIMETERS, METERS, CENTIMETERS,
KILOMETERS} ;
switch( x )
{
case INCHES:
switch( y )
{
case FEET:
InchesToFeet(); break;
case YARDS:
InchesToYards(); break;
case MILES:
InchesToMiles(); break;
case MILLIMETERS:
InchesToMillimeters(); break;
case CENTIMETERS:
InchesToCentimeters(); break;
case METERS:
InchesToMeters(); break;
case KILLOMETERS:
InchesToKilometers(); break;
} // End Switch( y ) with x == INCHES
break;
case FEET:
switch( y )
{
case INCHES:
FeetToInches(); break;
} // End Switch( y ) with x == FEET
break;
} // End Switch( x )
enum units {INCHES = 1, FEET, YARDS, MILES,
MILLIMETERS, METERS, CENTIMETERS,
KILOMETERS} ;
switch( x )
{
case INCHES:
switch( y )
{
case FEET:
InchesToFeet(); break;
case YARDS:
InchesToYards(); break;
case MILES:
InchesToMiles(); break;
case MILLIMETERS:
InchesToMillimeters(); break;
case CENTIMETERS:
InchesToCentimeters(); break;
case METERS:
InchesToMeters(); break;
case KILLOMETERS:
InchesToKilometers(); break;
} // End Switch( y ) with x == INCHES
break;
case FEET:
switch( y )
{
case INCHES:
FeetToInches(); break;
} // End Switch( y ) with x == FEET
break;
} // End Switch( x )
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement