tags:

views:

372

answers:

8
int number;
cin>>number;

switch (number)
{
    case 1:
            cout<<"My Favourite Subject is";
            break;
    case 2:
            cout<<"Fundamentals of Programming";
            break;
    case 3:
            cout<<"Exit";
            break;
    default:
            cout<<"Invalid Data";
}
+1  A: 

Check whether number is equal to the first value from switch, if equal then output text, otherwise(else) check next number.

if ( number == /*put here value to check*/ )
  // print some text
else
  // do something else
Kirill V. Lyadvinsky
+4  A: 

You replace switch statement with if-else

if (number == 1)
{
}
else if (number == 2)
{
}
...
{
}
else
{
    // default here
}
stefanB
I like that you provided some help but not the exact answer...
marcc
I tried, I'm nice person. I try to help new developers to get as much copy/paste practice as possible - the less they use their heads the less they learn, the less they learn the less competition in market place ... I might be able to get a job even when I'm old ...
stefanB
A: 

Replace the case statement with an if statement:

if (number == 1) {
    cout<<"My Favourite Subject is";
} else if (number == 2) {
    cout<<"Fundamentals of Programming";
} else if (number == 3) {
    cout<<"Exit";
} else {
    cout<<"Invalid Data";
}
David Harris
-1 for doing homework
big-z
you don't know if it is homework, the developer might be really stack coding his next twitter server and really need to get over the part of replacing switch with if-else ...
stefanB
+1 for trying to help
dalle
A: 

if (number == 1) {
cout << "blah1";
}
else if (number == 2) {
cout << "blah2";
}
else if (number == 3) {
cout << "blah3";
}
else { cout << "default";
}

scubabbl
-1 for doing homework
big-z
+1 for trying to help
dalle
+1  A: 

This is my favorite, even though it is not what you asked for:

string res =
  number==1 ? "My Favourite Subject is" :
  number==2 ? "Fundamentals of Programming" :
  number==3 ? "Exit" :
  number==4 ? "Invalid Data" :
  "";

cout<<res;

The good side here is that you don't have to constrain yourself to integer comparison. Instead of number==1 you can use any kind of complexComparisonReturningBoolean(number).

AareP
Use `const char*` instead `string` to avoid string copying.
Kirill V. Lyadvinsky
I actually find it hard to read more than one ? operator in a single expression.
asveikau
It's easier to understand if you forget how ?-operator normally works. Here it defines a table with two columns - column of conditions and column of values.
AareP
A: 

Try :
if (number < 1 || number > 3) {
//
} else if (number == 1) {
//
} else if (number/2 == 1) {
//
} else if ((number - 1)/ 2 == 1) {
//
}
This helps you get more math expertise than just checking for equality.

fastcodejava
+2  A: 
//Hey this is fun!
int number;
cin>>number;

// ultra const!
static const char const * const table[] =
{
  "Invalid Data",
  "My Favourite Subject is",
  "Fundamentals of Programming",
  "Exit"
};

cout<<table[number&3];

//Who needs if statements!!?
tony
+1  A: 

Also just for fun: Just use capital letters and semi-colon instead of colon. Ah, and don't forget to add an evil macro :)

#define SWITCH(s) for(int switch_=s, b=1;b;b=0) {
#define CASE(n) } if ( switch_ == n ) {
#define DEFAULT }

int number;
cin>>number;

SWITCH(number)
{
 CASE(1);
  cout << "My Favourite Subject is";
  break;
 CASE(2);
  cout << "Fundamentals of Programming";
  break;
 CASE(3);
  cout << "Exit";
  break;
 DEFAULT;
  cout << "Invalid Data";
}

This kill the 'switch' and if 'for' loop are not allowed, it is also possible to use a BREAK macro, but it is even more evil.

Alink
it might be better if you used gotos for your breaks/BREAKs.
tony
but with a normal switch statement in C you can write: case 0: if (blah) case 1: { foo(); }And then case 0 will do foo() if blah is true, whereas case 1 will always do foo(). This doesn't work with your macros.
asveikau
@tony: yeah, I thought about that too, but where do you put the exit label ? Also, I wanted to avoid the hassle of generating unique label and passing it between macros.
Alink
@asveikau: indeed but I was not aware that such messy imbrication of cases was allowed. This is why I like this kind of exercise, you always learn weird little syntax features (thanks btw). I will not try to adapt it to support all of those, though.
Alink