Switch Statement

Question: The program is about to compare any two numbers. Then, it will display whether the number is minimum or maximum. Please use switch statement.

Here's my assignment question. I kinda need help on how to solve this. PLEASE GUYS!
compare by if()
Can somebody show me a sample please? I'm really confused.

I HAVE TO USE SWITCH!!! IT's PART OF THE QUESTION
// this is a cpp program
// your complete program is
#include <iostream>


using namespace std;

int main(){
int x;
int y;
cout<<"enter x:";
cin>>x;
cout<<"enter y:";
cin>>y;

switch(x>y){
case true:
cout<<x<<" is max and "<<y<<" is min"<<endl;
break;

case false:
cout<<x<<" is min and "<<y<<" is max"<<endl;
break;

}

return 0;
}


Thanks :)
closed account (z05DSL3A)
sujitnag,

And if x == y, then what?
#include <iostream>


using namespace std;

int main(){
int x;
int y;
cout<<"enter x:";
cin>>x;
cout<<"enter y:";
cin>>y;

switch(x>y){
case true:
cout<<x<<" is max and "<<y<<" is min"<<endl;
break;

case false:
switch(x==y){
case true: cout << "x ==y"<<endl;
break;
case false:
cout<<x<<" is min and "<<y<<" is max"<<endl;
break;

}

}

return 0;
}
closed account (z05DSL3A)
Wow, that's ugly.
this is called nested.

are you new in c/c++?
closed account (z05DSL3A)
are you new in c/c++?

Errm, No.

Edit:
P.S. use [code][/code] tags please.

It will still be ugly code but a bit easier to read.
Last edited on
so, I hope some clear code in this program from you.

I think, if else if is the best chose for this program, but a/c to condition i made this code.
> I HAVE TO USE SWITCH!!! IT's PART OF THE QUESTION

1
2
3
4
5
6
7
8
9
10
11
12
13
switch( (x>y) - (y>x) ) // int x, int y
{
      case 1 : 
            // x is larger than y
            break ;

      case 0 : 
            // x is equal to y
            break ;

      default: // -1
           // x is smaller than y 
}
Topic archived. No new replies allowed.