Thread: Switch case loop

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    14

    Switch case loop

    Code:
    int screen3 (initial_amount)
    {
    	//Variable
    	int temperature;
    	int light_intensity;
    	int added_amount;
    	int days_of_idle;
    	int function;
    	int main_menu;
    
    	temperature = 30;
    	light_intensity = 2500;
    
    	system ("cls");
    	
    
    	printf("(1) Adjust the temperature of the culture. \n");
    	printf(" (2) Adjust the light intensity of the culture.\n");
    	printf("(3) Add new amount of Lactobacillus into\n");
    	printf("      the culture.\n");
    	printf("(4) Add random amount of Lactobacillus into\n");
    	printf("      the culture.\n");
    	printf("(5) Let the culture be idle for one day. \n");
    	printf("(6) Let the culture be idle for several days\n");
    	printf("      (maximum of 5 days)\n");
    
    
    	printf ("Current amount of Lactobacillus: %d.\n",initial_amount);
    	printf ("Current culture temperature: %d degree.\n",temperature);
    	printf ("Current light intensity: %d.\n\n\n",light_intensity);
    	printf ("Please select a function:");
    	scanf ("%d",&function);
    
    	switch (function)
    	{
    	case 1:
    			screen4 (temperature,initial_amount,light_intensity);
    			break;
    		
    	case 2:
    			screen5 (temperature,initial_amount,light_intensity);
    			break;
    
    	case 3:
    			screen6 (temperature,initial_amount,light_intensity);
    			break;
    
    	case 4:
    			screen7 (temperature,initial_amount,light_intensity);
    			break;
    		
    	case 5:
    			screen8 (temperature,initial_amount,light_intensity);
    			break;
    
    	case 6:
    			screen9 (temperature,initial_amount,light_intensity);
    			break;
    
    
    	default:
    			printf ("Invalid Input, Only 1,2,3,4,5,6,7 is selectable.\n");
    			printf ("Please reselect a function:");
    			scanf ("%d",&function);
    			break;
    	}
    }

    My problem is how to I loop my default? I want the program to repeat if the user enter the function not equal to 1 to 7...
    Thanks for the help...

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Wrap the whole switch case statement inside a do/while loop and use a flag. Alternatively you could check for values < 1 and > 7, but that would make your switch/case somewhat redundant.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    err... what do you mean actually? I'm still new to programming... Can you can provide an example? Thanks...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by justin8077 View Post
    err... what do you mean actually? I'm still new to programming... Can you can provide an example? Thanks...
    Seriously...
    You can write the code above, but you can't figure out how to type "do" before and "while()" after the switch statement?

    What should we take from that?

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    sry its my mistake... i see the sentences wrongly...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  3. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM
  4. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM
  5. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM