how to loop in switch??

This is a discussion on how to loop in switch?? within the C++ Programming forums, part of the General Programming Boards category; how im gonna loop menu if option not 1 or 2 ??? Code: void display( ) { int option; system("cls"); ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    Arrow how to loop in switch??

    how im gonna loop menu if option not 1 or 2 ???
    Code:
    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
    	  }
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Something like this:

    Code:
    int menu()
    {
      cout<< bla bla;
      cin >>option;
      return option;
    }
    
    int option = menu();
    switch ( option ) 
    	  { ....}
    and put a break; after default also.
    Last edited by Ducky; 05-03-2009 at 04:44 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    how to return in void??

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,530
    Code:
       return;
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    Arrow

    Quote Originally Posted by grumpy View Post
    Code:
       return;
    with return; or without that its returning to Main.
    i wan to return inside the void. so user can enter right option number

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by pczafer View Post
    how im gonna loop menu if option not 1 or 2 ???
    How I'm gonna respond to that: (Yes that form of sentence is a statement not a question, regardless of how many superflous question marks you decorate it with)

    Judging by the "gonna" used here, your first language probably is actually English. However, since you've put zero effort into writing anything resembling a well written question, I'm going to put zero effort into providing the kind of response you were probably hoping for.
    Unless your posting style improves dramatically, you'll be added to my list of posters whose threads I won't even read so much as the title of any more.

    'Nuff said!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. break statement not within loop or switch
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:36 AM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  4. Switch in a While Loop
    By WindShield in forum C Programming
    Replies: 6
    Last Post: 10-19-2002, 01:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21