how to loop in switch??

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 24
Reputation: pczafer is an unknown quantity at this point 
Solved Threads: 0
pczafer pczafer is offline Offline
Newbie Poster

how to loop in switch??

 
0
  #1
May 3rd, 2009
how im gonna loop menu if option not 1 or 2 ???
  1. void display( )
  2. {
  3. int option;
  4. system("cls"); //clear screen
  5. cout<<endl<<endl;
  6. cout<<"\t * Display Menu *\n";
  7. cout<<"\n\t1. Display For Certein Date"<<endl;
  8. cout<<"\t2. Display All Appointments"<<endl;
  9. cout<<"\nEnter Option Number: "; //Enter option
  10. cin>>option;
  11. cin.get();
  12.  
  13. switch ( option )
  14. {
  15. case 1: displayCerteinDate ( );
  16. break;
  17. case 2: displayAll ( );
  18. break;
  19. default: cout<<endl<<"Wrong option number!! Try again\n";
  20. cin.get(); //give the user a chance to read the output data
  21. }
  22. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: how to loop in switch??

 
0
  #2
May 3rd, 2009
Put
  1. cout<<endl<<"Wrong option number!! Try again\n";
  2. 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:
  1. do {
  2. cout<<endl<<"Wrong option number!! Try again\n";
  3. cin>>option;
  4. } 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."
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how to loop in switch??

 
0
  #3
May 3rd, 2009
  1. while ( cin>>option ) {
  2. switch ( option )
  3. {
  4. }
  5. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: how to loop in switch??

 
0
  #4
May 3rd, 2009
Originally Posted by Salem View Post
  1. while ( cin>>option ) {
  2. switch ( option )
  3. {
  4. }
  5. }
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: how to loop in switch??

 
-1
  #5
May 3rd, 2009
actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )

  1.  
  2. void display( )
  3. {
  4. int option, int flag =1;
  5. system("cls"); //clear screen
  6. cout<<endl<<endl;
  7. cout<<"\t * Display Menu *\n";
  8. cout<<"\n\t1. Display For Certein Date"<<endl;
  9. cout<<"\t2. Display All Appointments"<<endl;
  10. cout<<"\t3. exit"<<endl;
  11. cout<<"\nEnter Option Number: "; //Enter option
  12. cin>>option;
  13. cin.get();
  14. while(flag)
  15. {
  16. switch ( option )
  17. {
  18. case 1: displayCerteinDate ( );
  19. break;
  20. case 2: displayAll ( );
  21. break;
  22. case 3: flag =0;
  23. break;
  24. default: cout<<endl<<"Wrong option number!! Try again\n";
  25. cin.get(); //give the user a chance to read the output data
  26. }
  27. }
  28.  
  29. }


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" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: how to loop in switch??

 
0
  #6
May 3rd, 2009
Originally Posted by rahul8590 View Post
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 ?
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: how to loop in switch??

 
0
  #7
May 3rd, 2009
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.....
Last edited by rahul8590; May 3rd, 2009 at 7:03 am.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: how to loop in switch??

 
0
  #8
May 3rd, 2009
Originally Posted by rahul8590 View Post
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?
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: how to loop in switch??

 
0
  #9
May 3rd, 2009
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............
Last edited by rahul8590; May 3rd, 2009 at 7:07 am.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: how to loop in switch??

 
0
  #10
May 3rd, 2009
Originally Posted by rahul8590 View Post
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC