954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to loop in switch??

how im gonna loop menu if option not 1 or 2 ???

void display( )
{  
	int option;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();

	switch ( option ) 
	  {
		 case 1: displayCerteinDate ( );
			     break;
		 case 2: displayAll ( );
		             break;
	     default: cout<<endl<<"Wrong option number!! Try again\n";
	              cin.get();         //give the user a chance to read the output data
	  }
}
pczafer
Light Poster
38 posts since Apr 2009
Reputation Points: 12
Solved Threads: 0
 

Put

cout<<endl<<"Wrong option number!! Try again\n";
cin.get(); //give the user a chance to read the output data

in a do-while -loop and change cin.get(); to cin>>option; like this:

do {
    cout<<endl<<"Wrong option number!! Try again\n";
    cin>>option;
} while(option<1 || option>2);
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
while ( cin>>option ) {
    switch ( option )
    {
    }
}
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
while ( cin>>option ) {
    switch ( option )
    {
    }
}


You have to take into account that the loop won't end automatically except if the user enters some text ...

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )

void display( )
{  
	int option, int flag =1;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\t3.  exit"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();
    while(flag)
	{   
        switch ( option ) 
        {
            case 1: displayCerteinDate ( );
                    break;
            case 2: displayAll ( );
		             break;
            case 3: flag =0;
                     break;
            default: cout<<endl<<"Wrong option number!! Try again\n";
                    cin.get();         //give the user a chance to read the output data
	  }
	}

}

i have intialised a variable flag to 1 . and put the whole swicth inside a while loop which check for the status of the flag.
i have included an exit option in the menu . so as soon as the user presses 3. the flag =0 and the while condition fails , eventually u will come out of the loop and program ends

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 
actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )

Why the h*ll would you use an integer flag? Isn't boolean good enough here ?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

flag is a variable name .. u can also write ur name instead of that.. its just that i am a bit convenient with that name that's its.....
and i am also giving an option in switch case if the user wants to exit.
both the way it works.....

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 
flag is a variable name .. u can also write ur name instead of that.. its just that i am a bit convenient with that name that's its.....

I know flag is a variable name, but two things, if you would have to use a flag in this case you should have used a boolean instead of an integer and here it's just overkill, why would you use an integer flag here?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

i guess u didnt understand my code..... why dont you execute this code and see how exactly it works.......
and variable can also be put inside instead of boolean, any way 1= TRUE and and 0 or lesser implies false.......

and if you have observed the original code.. it doesnt have ne room for allowing the user to exit..
if he./she presses the wrong number , its just wants to accept the new number .. so where the h@#l the user will exit............

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 
i guess u didnt understand my code..... why dont you execute this code and see how exactly it works....... and variable can also be put inside instead of boolean, any way 1= TRUE and and 0 or lesser implies false.......


I know, you don't have to teach me C++, but a boolean takes up less memory :)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
while ( cin>>option ) {
    switch ( option )
    {
    }
}


i need

default: cout<<endl<<"Wrong option number!! Try again\n";


as well!!!

pczafer
Light Poster
38 posts since Apr 2009
Reputation Points: 12
Solved Threads: 0
 

What about the following:

do {
    cin>>option;
    switch ( option ) 
    {
	case 1:
            displayCerteinDate();
	    break;
	case 2:
            displayAll();
	    break;
	default:
            cout<<endl<<"Wrong option number!! Try again\n";
    }
} while (!(1<= option && option<=2));
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )

void display( )
{  
	int option, int flag =1;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\t3.  exit"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();
    while(flag)
	{   
        switch ( option ) 
        {
            case 1: displayCerteinDate ( );
                    break;
            case 2: displayAll ( );
		             break;
            case 3: flag =0;
                     break;
            default: cout<<endl<<"Wrong option number!! Try again\n";
                    cin.get();         //give the user a chance to read the output data
	  }
	}

}

i have intialised a variable flag to 1 . and put the whole swicth inside a while loop which check for the status of the flag. i have included an exit option in the menu . so as soon as the user presses 3. the flag =0 and the while condition fails , eventually u will come out of the loop and program ends


i couldent get it why you made liket his but any way i tried its looping but only"Wrong option number!! Try again\n";
even if i enter 1 or 2 after one wrong option its still looping same. and it effected option 1 and 2 as well after they display result not returning main. looping forever

pczafer
Light Poster
38 posts since Apr 2009
Reputation Points: 12
Solved Threads: 0
 

Did you even read my previous post ?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
Did you even read my previous post ?


yes i was trying your codes but ist giving errorError 1 error C2059: syntax error : '}' c:\visual studio 2005\projects\wer\wer\wer.cpp 278
looks fine to me but giging this error.

pczafer
Light Poster
38 posts since Apr 2009
Reputation Points: 12
Solved Threads: 0
 
yes i was trying your codes but ist giving error looks fine to me but giging this error.

Then you must have done something wrong, I tested it and it was working perfectly :)

Could you please post the whole code which was causing this error?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
void display( )
{          nt option;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();
	do { 
	          cin>>option;
	        switch ( option ) 
	         {
		 case 1: displayCerteinDate ( );
			     break;
		 case 2: displayAll ( );
		              break;
	         default: cout<<endl<<"Wrong option number!! Try again\n";
	                  cin.get();         //give the user a chance to read the output data
	          }
	while (!(1<=option && option<=2));
	  }
}
pczafer
Light Poster
38 posts since Apr 2009
Reputation Points: 12
Solved Threads: 0
 

Change

while (!(1<=option && option<=2));
	  <strong>}</strong>


to

<strong>}</strong>
while (!(1<=option && option<=2));


nt option; has to be int option; :)

the following:

....
        cin>>option;
	cin.get();
	do { 
....


should be changed to:

....
	do { 
....

and...

.....
default: cout<<endl<<"Wrong option number!! Try again\n";
	                  cin.get(); //give the user a chance to read the output data
	          }
.....


has to be:

.....
default: cout<<endl<<"Wrong option number!! Try again\n";
	          }
.....


:P

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Thanks tux4life you are the best
its done.

pczafer
Light Poster
38 posts since Apr 2009
Reputation Points: 12
Solved Threads: 0
 

It was actually good old Salem who brought me on the idea :P

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You