Like Tree3Likes
  • 1 Post By Yogindar Das
  • 2 Post By iMalc

do / while loop

This is a discussion on do / while loop within the C Programming forums, part of the General Programming Boards category; How should this switch statement be correctly rapped into a do while loop? I need the switch statement to loop ...

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    do / while loop

    How should this switch statement be correctly rapped into a do while loop? I need the switch statement to loop until the user selects a correct option.


    Code:
    		scanf("%d", &options);
    	
    	switch (options)
    	{
    		case 1 :
    		{
    			sumIntegers ();
    			break;
    		}
    		case 2 :
    		{
    			sumDoubles ();
    			break;
    		}
    		case 3 :
    		{
    			calcFactorial ();
    			break;
    		}
    		default :
    		{
    			printf("invalid choice. Please choose correctly.\n");
    		}
    	}

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    73
    deleted
    Last edited by progmateur; 03-23-2013 at 01:58 PM.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Bangalore
    Posts
    4
    Code:
    do
            {
            unknownoption=0;
    	scanf("%d", &options);
    	
    	switch (options)
    	{
    		case 1 :
    		{
    			sumIntegers ();
    			break;
    		}
    		case 2 :
    		{
    			sumDoubles ();
    			break;
    		}
    		case 3 :
    		{
    			calcFactorial ();
    			break;
    		}
    		default :
    		{
    			printf("invalid choice. Please choose correctly.\n");
                            unknownoption=1; 
                    }
                }
                }while( unknownoption );
    progmateur likes this.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    One way I like to do it:
    Code:
    do {
    	scanf("%d", &options);
    	switch (options)
    	{
    		case 1 :
    		{
    			sumIntegers ();
    			break;
    		}
    		case 2 :
    		{
    			sumDoubles ();
    			break;
    		}
    		case 3 :
    		{
    			calcFactorial ();
    			break;
    		}
    		default:
    			printf("invalid choice. Please choose correctly.\n");
    			continue;
    	}
    } while (0);
    Otherwise known as a glorified goto, but hey works for me.
    progmateur and Crossfire like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by iMalc View Post
    One way I like to do it:
    Code:
    do {
    	scanf("%d", &options);
    	switch (options)
    	{
    		case 1 :
    		{
    			sumIntegers ();
    			break;
    		}
    		case 2 :
    		{
    			sumDoubles ();
    			break;
    		}
    		case 3 :
    		{
    			calcFactorial ();
    			break;
    		}
    		default:
    			printf("invalid choice. Please choose correctly.\n");
    			continue;
    	}
    } while (0);
    Otherwise known as a glorified goto, but hey works for me.
    I'd prefer to refactor the menu driver into a separate function that validates input and returns the validated input. Then this becomes a simple switch statement without the need of looping. That's more reusable too - if you make a generic menu driver, you can reuse it for other menus, plus it separates the different concerns of the program (collecting and validating input is separate from acting on the input).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 09:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 06:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 09:52 AM

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