13 Replies - 699 Views - Last Post: 30 May 2010 - 02:20 PM Rate Topic: -----

#1 maxpower1991  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 33
  • Joined: 21-May 10

switch and if

Posted 28 May 2010 - 12:05 PM

can I use if statement in said switch ???
in this code :
main(){
int z;
while(z!=3)
{
cout<<"\n...............MENU.................\n\n";
cout<<"Enter.....(1)\n";
cout<<"Enter.....(2)\n";
cout<<"Enter.....(3)\n";
cin>>z;
switch(z)
{
//some statement.......
} 

break;
case 2:
{
//some statement.......
} 

break;
}}



and I don't want to enter 2 unless enter 1
???

help please and thanks

Is This A Good Question/Topic? 0
  • +

Replies To: switch and if

#2 Korupt  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 185
  • Joined: 22-June 08

Re: switch and if

Posted 28 May 2010 - 01:00 PM

With switch:
switch(z)
{
	case 1:
		//Option 1
	break;

	case 2:
		//Option 2
	break;

	case 3:
		//Option 3
	break;

	default:
		//None of the options
	break;
}


With if:
if (z == 1)
{
	//Option 1
}
else if (z == 2)
{
	//Option 2
}
else if (z == 3)
{
	//Option 3
}
else
{
	//None of the options
}

This post has been edited by Korupt: 28 May 2010 - 01:01 PM

Was This Post Helpful? 1
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: switch and if

Posted 28 May 2010 - 01:25 PM

I dont really get what you are trying to ask. Can you clarify?
Was This Post Helpful? 0
  • +
  • -

#4 maxpower1991  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 33
  • Joined: 21-May 10

Re: switch and if

Posted 28 May 2010 - 02:03 PM

View PostAphex19, on 28 May 2010 - 07:25 PM, said:

I dont really get what you are trying to ask. Can you clarify?


I don't want the user to get inside case 2: case 3: case 4: and so on
before Enter case 1:

because every thing depend on case 1:

what the way can I use to do that?
Was This Post Helpful? 0
  • +
  • -

#5 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: switch and if

Posted 28 May 2010 - 02:11 PM

Sounds like you don't need a switch at all, sounds to me like all you need are nested if checks

if (blah > 0)
{
    if (blah > 3)
    {
        if (blah > 4)
        {
            // do something...
        }
    }
    else if (blah > 2)
    {
        // do something...
    }
}



Unless I'm completely misunderstanding your question, as far as I know, there isn't a way to allow a switch to work the way you're describing.

Or another way could be to have an if check before the switch, and have the switch inside the if check, then the switch wouldn't be accessed unless a specific thing were true.

if (blah > 3)
{
    switch(blah)
    {
        case 0:
            // do something...
            break;
        case 1:
            // do something different...
            break;
        default:
            break;
    }
}


This post has been edited by Kilorn: 28 May 2010 - 02:15 PM

Was This Post Helpful? 1
  • +
  • -

#6 maxpower1991  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 33
  • Joined: 21-May 10

Re: switch and if

Posted 29 May 2010 - 08:42 AM

View PostKilorn, on 28 May 2010 - 08:11 PM, said:

Sounds like you don't need a switch at all, sounds to me like all you need are nested if checks

if (blah > 0)
{
    if (blah > 3)
    {
        if (blah > 4)
        {
            // do something...
        }
    }
    else if (blah > 2)
    {
        // do something...
    }
}



Unless I'm completely misunderstanding your question, as far as I know, there isn't a way to allow a switch to work the way you're describing.

Or another way could be to have an if check before the switch, and have the switch inside the if check, then the switch wouldn't be accessed unless a specific thing were true.

if (blah > 3)
{
    switch(blah)
    {
        case 0:
            // do something...
            break;
        case 1:
            // do something different...
            break;
        default:
            break;
    }
}





thank you so so so so much
you give me an idea to solve my problem
thank u :D :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: switch and if

Posted 29 May 2010 - 01:28 PM

why not just use a boolean to flag if the user has already entered one?
Was This Post Helpful? 0
  • +
  • -

#8 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: switch and if

Posted 29 May 2010 - 03:35 PM

Or you can have it default to one, and fall through to 2, 3, 4 by omitting the break; statement after case 1... probably terrible practice but would work.
Was This Post Helpful? 0
  • +
  • -

#9 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: switch and if

Posted 29 May 2010 - 04:55 PM

You could use a BOOL value.

bool 1_IsPicked = false;

main(){
int z;
while(z!=3)
{
cout<<"\n...............MENU.................\n\n";
cout<<"Enter.....(1)\n";
cout<<"Enter.....(2)\n";
cout<<"Enter.....(3)\n";
cin>>z;
switch(z)
{

case 1:
{
1_IsPicked = true;
// Some stuff
}
break;

case 2:
{
if(1_IsPicked == true)
{
// Do your stuff
}
}
break;
} 
}


This post has been edited by BlueMelon: 29 May 2010 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#10 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: switch and if

Posted 29 May 2010 - 11:55 PM

Variable names can't start with numbers, BlueMelon.
Was This Post Helpful? 0
  • +
  • -

#11 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: switch and if

Posted 30 May 2010 - 06:23 AM

It was meant for an example, sarmanu.

This post has been edited by BlueMelon: 30 May 2010 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#12 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: switch and if

Posted 30 May 2010 - 06:27 AM

I knew you would say that. When you provide an example, at least use valid syntax. Otherwise, show him pseudocode.
Was This Post Helpful? 0
  • +
  • -

#13 DarkX411  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 10-May 10

Re: switch and if

Posted 30 May 2010 - 06:43 AM

Wouldn't switch only be useful if you're using a big list, cause doesn't use nested if-else's get a little inefficient after awhile?
Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: switch and if

Posted 30 May 2010 - 02:20 PM

well heres another example by just checking the conditions
int menu(void)
{
	int input;
	cout<<"\n...............MENU.................\n\n";
	cout<<"Enter.....(1)\n";
	cout<<"Enter.....(2)\n";
	cout<<"Enter.....(3)\n";
	cout<<"QUIT......(4)\n";
	cin>>input;
	return input;
}

int main()
{
	int choice = 0;
	while((choice = menu())!=1) { cerr<<"You must select 1 first!"<<endl; }
	
	do{
		switch(choice)
		{
		case 1: cout<<"SELECTED 1"<<endl; break;
		case 2: cout<<"SELECTED 2"<<endl; break;
		case 3: cout<<"SELECTED 3"<<endl; break;
		default: cout<<"INPUT ERROR"<<endl;
		}
		
	}while((choice = menu())!=4);

	return 0;
}


This post has been edited by ImaSexy: 30 May 2010 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1