Thread: how to loop in switch??

  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
    930
    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 03:44 AM.
    Using Windows 10 with Code Blocks and MingW.

  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,815
    Code:
       return;
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, 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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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, 01:21 PM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  4. Switch in a While Loop
    By WindShield in forum C Programming
    Replies: 6
    Last Post: 10-19-2002, 12:04 AM