how can i check if the number is positive or not by using switch statement
please who know the answer help me and answer the question

Comments
Someone gave you -1 because he just lost his job as a computer consultant, so I gave you +1.

how can i check if the number is positive or not by using switch statement

Use the % operator in the SWITCH statement. You know what values you will get so use those values in the CASE statements.

please who know the answer help me and answer the question

Please don't be so pushy. this statement is completely unnecessary and can be read as rude. Just so you know.

how can i check if the number is positive or not by using switch statement

Use the % operator in the SWITCH statement. You know what values you will get so use those values in the CASE statements.

Walt, note that I am not the OP. I am getting tempted to send you a check for $85.

I cannot see how use the modulo operator is going to help much with determining whether a number is positive or not in a switch statement. This seems like an obvious case where an if statement make sense and a switch statement does not. What am I missing?

I would use abs() (from math.h) inside the switch statement (assuming you're dealing only with integers).

Edited 4 Years Ago by thines01: n/a

This isn't rocket science guys...

int i = -1;

bool IsPositive()
{
	if(i > 0)
		return true;
	else if(i < 0)
		return false;
}

void Example()
{
	switch(IsPositive())
	{
	case true:
		cout << "+" << endl;
	break;
	case false:
		cout << "-" << endl;
	break;
	case default:
		cout << "Zero/Unknown?" << endl;
	break;
	}
}
Comments
And yet again you do someone's homework for them. You just don't get it, do you...

Oops, made a tiny error.. fixed:

int i = -1;

bool IsPositive()
{
	if(i > 0)
		return true;
	else if(i < 0)
		return false;
}

void Example()
{
	switch(IsPositive())
	{
	case true:
		cout << "+" << endl;
	break;
	case false:
		cout << "-" << endl;
	break;
	default:
		cout << "Zero/Unknown?" << endl;
	break;
	}
}
This article has been dead for over six months. Start a new discussion instead.