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

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

Recommended Answers

All 10 Replies

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.

can you please write the code

Yes I can.

If you want me to do it for you, as a computer consultant I get $85 an hour.

commented: not worth it. +0

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).

Why not just use if?

int a;

if( a > 0 )
{
 positive
}else if( a < 0)
{
 negative
}

If the exercise is to actually use a switch statement, the actions you take are different.

When I get home, I'll write the code for you.

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;
	}
}
commented: And yet again you do someone's homework for them. You just don't get it, do you... -4

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;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.