943,695 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1307
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 3rd, 2009
0

how to loop in switch??

Expand Post »
how im gonna loop menu if option not 1 or 2 ???
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
pczafer is offline Offline
38 posts
since Apr 2009
May 3rd, 2009
0

Re: how to loop in switch??

Put
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 3rd, 2009
0

Re: how to loop in switch??

C++ Syntax (Toggle Plain Text)
  1. while ( cin>>option ) {
  2. switch ( option )
  3. {
  4. }
  5. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 3rd, 2009
0

Re: how to loop in switch??

Click to Expand / Collapse  Quote originally posted by Salem ...
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 3rd, 2009
-1

Re: how to loop in switch??

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
May 3rd, 2009
0

Re: how to loop in switch??

Click to Expand / Collapse  Quote originally posted by rahul8590 ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 3rd, 2009
0

Re: how to loop in switch??

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.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
May 3rd, 2009
0

Re: how to loop in switch??

Click to Expand / Collapse  Quote originally posted by rahul8590 ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 3rd, 2009
0

Re: how to loop in switch??

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.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
May 3rd, 2009
0

Re: how to loop in switch??

Click to Expand / Collapse  Quote originally posted by rahul8590 ...
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
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: itoa () Implementation Need Improvements
Next Thread in C++ Forum Timeline: Postfix calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC