Jump to content
  • Advertisement
Sign in to follow this  
Cadoink

If... Else in C++

This topic is 3654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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)
...

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!