| | |
how to loop in switch??
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 24
Reputation:
Solved Threads: 0
how im gonna loop menu if option not 1 or 2 ???
C++ Syntax (Toggle Plain Text)
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 } }
Put in a
C++ Syntax (Toggle Plain Text)
cout<<endl<<"Wrong option number!! Try again\n"; cin.get(); //give the user a chance to read the output data
do-while -loop and change cin.get(); to cin>>option; like this: C++ Syntax (Toggle Plain Text)
do { cout<<endl<<"Wrong option number!! Try again\n"; cin>>option; } while(option<1 || option>2);
Last edited by tux4life; May 3rd, 2009 at 6:49 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
You have to take into account that the loop won't end automatically except if the user enters some text ...
Last edited by tux4life; May 3rd, 2009 at 7:09 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )
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
C++ Syntax (Toggle Plain Text)
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
Last edited by rahul8590; May 3rd, 2009 at 6:57 am.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Last edited by tux4life; May 3rd, 2009 at 7:12 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
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.....
and i am also giving an option in switch case if the user wants to exit.
both the way it works.....
Last edited by rahul8590; May 3rd, 2009 at 7:03 am.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
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?
Last edited by tux4life; May 3rd, 2009 at 7:10 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
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............
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............
Last edited by rahul8590; May 3rd, 2009 at 7:07 am.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
•
•
•
•
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.......
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Similar Threads
- How to use while loop with switch and if statement (C#)
- exiting loop with switch (C++)
- Need help with do while loop (Java)
- Looking for a better switch without strings is there one? (C++)
- Infinite Loop in the switch statements (C++)
- program w/ switch AND nested if. six error messages (C++)
Other Threads in the C++ Forum
- Previous Thread: itoa () Implementation Need Improvements
- Next Thread: Postfix calculator
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






