Like Tree1Likes
  • 1 Post By quzah

how to stop the looping?

This is a discussion on how to stop the looping? within the C Programming forums, part of the General Programming Boards category; Code: int screen4 (temperature,initial_amount,light_intensity) { int temperature_level; system ("cls"); printf(" ************************************************************\n"); printf(" * Welcome to Lactobacillus Simulator *\n"); printf(" * ...

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

    how to stop the looping?

    Code:
    int screen4 (temperature,initial_amount,light_intensity)
    {
    	int temperature_level;
    	system ("cls");
    	
    	printf("          ************************************************************\n");
    	printf("          *            Welcome to Lactobacillus Simulator            *\n");
    	printf("          *                                                          *\n");
    	printf("          *                           Menu                           *\n");
    	printf("          *                                                          *\n");
    	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("          *        (7) Return to main menu.                          *\n");
    	printf("          *                                                          *\n");
    	printf("          *                                                          *\n");
    	printf("          ************************************************************\n\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 ("Temperature Selection:\n");
    	printf ("(1)  5 degree\n");
    	printf ("(2) 10 degree\n");
    	printf ("(3) 25 degree\n");
    	printf ("(4) 30 degree\n");
    	printf ("Please select a temperature level:");
    	scanf ("%d",&temperature_level);
    
    	do{
    	switch (temperature_level)
    	{
    	case 1:
    		temperature = 5;
    		printf("The temperature is adjusted to 5 degree\n");
    		break;
    
    	case 2:
    		temperature = 10;
    		printf("The temperature is adjusted to 10 degree\n");
    		break;
    
    	case 3:
    		temperature = 25;
    		printf("The temperature is adjusted to 25 degree\n");
    		break;
    
    	case 4:
    		temperature = 30;
    		printf("The temperature is adjusted to 30 degree\n");
    		break;
    
    	default:
    		printf("\n\nInvalid Input!!.\n");
    		printf("Please enter another option.\n");
    		printf("The option only consist of 1,2,3,4.\n\n");
    		printf("Please reselect a temperature level: ");
    		scanf("%d",&temperature_level); 
    		break;
    	}
    	}while(temperature_level >= 1 || temperature_level <= 4);
    	system ("pause");
    	screen10 (temperature,initial_amount,light_intensity);
    }
    Help... how to stop the looping?
    The program keep looping...

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you look at this line:

    Code:
    temperature_level >= 1 || temperature_level <= 4
    The condition is always true, as long as temperature_level is larger than 1 or smaller than 4. So it covers any value temperature_level can have. You want to check for values outside the range 1-4.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    Now i edit to temperature_level < 1 || temperature_level > 4. It can stop looping now but if i enter the value outside the range 1-4, the loop stop working edi.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by justin8077 View Post
    Now i edit to temperature_level < 1 || temperature_level > 4. It can stop looping now but if i enter the value outside the range 1-4, the loop stop working edi.
    Code:
    while(temperature_level >= 1 && temperature_level <= 4);
    I find it a little hard to believe you wrote such nice, well set up code and don't know how to use logical and and or.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The easiest way is probably to cut and paste a bit, if you enter a correct value in your default:, that will stop the loop since the value is now correct. If you instead move the original scanf inside the loop you wont have that problem.

    Quote Originally Posted by justin8077 View Post
    Code:
    do{
            printf ("Please select a temperature level:");
            scanf ("%d",&temperature_level);
    
    	switch (temperature_level)
    	{
    		case 1:
    		temperature = 5;
    		printf("The temperature is adjusted to 5 degree\n");
    		break;
    		
    		case 2:
    		temperature = 10;
    		printf("The temperature is adjusted to 10 degree\n");
    		break;
    		
    		case 3:
    		temperature = 25;
    		printf("The temperature is adjusted to 25 degree\n");
    		break;
    		
    		case 4:
    		temperature = 30;
    		printf("The temperature is adjusted to 30 degree\n");
    		break;
    		
    		default:
    		printf("\n\nInvalid Input!!.\n");
    		printf("Please enter another option.\n");
    		break;
    	}
    }while(temperature_level < 1 || temperature_level > 4);
    And you don't wan't && since the condition for the loop is when ever a value is outside the legal range.
    Last edited by Subsonics; 03-18-2011 at 09:01 AM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    If i use that, the program will keep running...
    ImageShack® - Online Photo and Video Hosting

    If i use back the code i edited... it look like this...
    ImageShack® - Online Photo and Video Hosting

    IF if i try the code above... IT show this...
    http://img200.imageshack.us/i/55703924.png/
    Last edited by justin8077; 03-18-2011 at 09:11 AM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I don't know post the code. If you just cut and paste this as a test, it should work.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int temperature_level = 0;
        int temperature;
    
    do{
            printf ("Please select a temperature level:");
            scanf ("%d",&temperature_level);
    
    	switch (temperature_level)
    	{
    		case 1:
    		temperature = 5;
    		printf("The temperature is adjusted to 5 degree\n");
    		break;
    		
    		case 2:
    		temperature = 10;
    		printf("The temperature is adjusted to 10 degree\n");
    		break;
    		
    		case 3:
    		temperature = 25;
    		printf("The temperature is adjusted to 25 degree\n");
    		break;
    		
    		case 4:
    		temperature = 30;
    		printf("The temperature is adjusted to 30 degree\n");
    		break;
    		
    		default:
    		printf("\n\nInvalid Input!!.\n");
    		printf("Please enter another option.\n");
    		break;
    	}
    }while(temperature_level < 1 || temperature_level > 4);
    
        return 0;
    }
    You asked this question a few days ago, I then suggested that you could use a flag, basically a variable you set to true or false. Here's the basic idea:

    Code:
    #include <stdbool.h>
    
    int main()
    {
        bool valid;
    
        do{
            valid = true;
    
            switch( value ) {
                case 1:
                    // do something
                    break;
                case 2:
                    // do something else
                    break;
                default:
                    // give an error
                    valid = false;
            }
        }while( valid == false );
    
        return 0;
    }
    Last edited by Subsonics; 03-18-2011 at 09:24 AM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Good lord....
    You should only need 1 copy of the menu display and 1 copy of your function switch to service the entire program.

    Make things like "Change Temperature" into a single function called from the central switch statement then incorporate the return value into your calculations. Don't keep re-running the program for every single user input.

    EDIT: I see the full listing has been removed.
    Last edited by CommonTater; 03-18-2011 at 10:16 AM.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    This is whats u mean right?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    //Function Prototype
    int menu (int temperature,int initial_amount,int light_intensity);
    int adjust_temperature (temperature_level);
    int adjust_light_intensity (light_intensity_level);
    
    //Main Menu
    int main (void)
    {
    	//variable
    	int user_choice; 
    
    	printf("          ************************************************************\n");
    	printf("          *            Welcome to Lactobacillus Simulator            *\n");
    	printf("          *                                                          *\n");
    	printf("          *        This program is use to simulate the growth        *\n");
    	printf("          *          of Lactobacillus in a control culture.          *\n");
    	printf("          *                                                          *\n");
    	printf("          *                                                          *\n");
    	printf("          *                                                          *\n");
    	printf("          * Copyright (C) Cheng Chin Pang, Lai Yin Yung              *\n");
    	printf("          *               Phoong Hui Ru, Tan Chin Wei                *\n");
    	printf("          ************************************************************\n\n");
    
    	printf("                                 Simulator Menu\n");
    	printf("                               1. Start Simulator\n");
    	printf("                               2. Close Simulator\n\n");
    	printf("                       Please select an option (1 or 2): ");
    	scanf("%d",&user_choice);
    
    	//For Error Option Entered
    	while( user_choice < 1 || user_choice > 2)
    	{
    		printf("\n\n                              An error had occur.\n");
    		printf("                          Please enter another option.\n");
    		printf("                      The option only consist of 1 and 2.\n\n");
    		printf("                      Please reselect an option (1 or 2): ");
    		scanf("%d",&user_choice);
    	}
    
    	//For Close Simulator
    		if ( user_choice == 2) 
    		{
    			printf("\n            Thank You For Using This Simulator. Have A Nice Day. ^.^\n\n");
    			system ("pause");
    			return 0;
    		}
    
    	//For Start Simulator
    	else 
    	{
    		add_initial_amount ();
    	}
    }
    
    //Add Initial Amount
    int add_initial_amount (void)
    {
    	//Variable
    	int initial_amount;
    	int temperature = 10;
    	int light_intensity = 2500;
    	system ("cls");
    
    	printf("          ************************************************************\n");
    	printf("          *            Welcome to Lactobacillus Simulator            *\n");
    	printf("          *                                                          *\n");
    	printf("          *        Lactobacillus is a genus of Gram-positive         *\n");
    	printf("          *        facultative anaerobic or microaerophilic.         *\n");
    	printf("          *         They are a major part of the lactic acid         *\n");
    	printf("          *        bacteria group, named as such because most        *\n");
    	printf("          *         of its members convert lactose and other         *\n");
    	printf("          *        sugars to lactic acid. They are common and        *\n");
    	printf("          *        usually benign. In humans they are present        *\n");
    	printf("          *       in the vagina and the gastrointestinal tract,      *\n");
    	printf("          *       where they are symbiotic and make up a small       *\n");
    	printf("          *        portion of the gut flora. Many species are        *\n");
    	printf("          *         prominent in decaying plant material. The        *\n");
    	printf("          *     production of lactic acid makes its environment      *\n");
    	printf("          *     acidic, which inhibits the growth of some harmful    *\n");
    	printf("          *      bacteria. Several members of the genus have had     *\n");
    	printf("          *                 their genome sequenced.                  *\n");
    	printf("          ************************************************************\n\n");
    
    	//Enter Amount of Lactobacillus
    	printf("Please enter the initial amount of Lactobacillus (Maximum amount is 500000000):\n");
    	printf(":");
    	scanf ("%d",&initial_amount);
    
    	//For Error Option Entered
    	while (initial_amount > 500000000 || initial_amount < 0 )
    	{
    		printf ("The maximum amount of Lactobacillus can enter is 500000000.\n");
    		printf ("Please re-enter the initial amount of Lactobacillus:\n");
    		scanf ("%d",&initial_amount);
    	}
    
    		printf ("The amount of Lactobacillus you enter is %d.\n",initial_amount);
    		system ("pause");
    		menu (temperature, initial_amount,light_intensity);
    }
    
    //Menu
    int menu (int temperature,int initial_amount,int light_intensity)
    {
    	//Variable
    	int function;
    	int temperature_level;
    	int light_intensity_level;
    
    	system ("cls");
    
    	printf("          ************************************************************\n");
    	printf("          *            Welcome to Lactobacillus Simulator            *\n");
    	printf("          *                                                          *\n");
    	printf("          *                           Menu                           *\n");
    	printf("          *                                                          *\n");
    	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("          *        (7) Return to main menu.                          *\n");
    	printf("          *                                                          *\n");
    	printf("          *                                                          *\n");
    	printf("          ************************************************************\n\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)
    		{
    			//Adjust Temperature
    		case 1:
    			printf ("Temperature Selection:\n");
    			printf ("(1)  5 degree\n");
    			printf ("(2) 10 degree\n");
    			printf ("(3) 25 degree\n");
    			printf ("(4) 30 degree\n");
    			printf ("Please select a temperature level:");
    			scanf ("%d",&temperature_level);
    
    			printf("The temperature is adjusted to %d\n\n",adjust_temperature (temperature_level));
    			temperature = adjust_temperature (temperature_level);
    			system ("pause");
    			menu (temperature, initial_amount,light_intensity);
    
    			//Adjust Light Intensity
    		case 2:
    			printf ("Light Intensity Selection:\n");
    			printf ("(1) 2500\n");
    			printf ("(2) 5000\n");
    			printf ("Please select a light intensity level:");
    			scanf ("%d",&light_intensity_level);
    
    			printf("The light intensity is adjusted to %d\n\n",adjust_light_intensity (light_intensity_level));
    			light_intensity = adjust_light_intensity (light_intensity_level);
    			system ("pause");
    			menu (temperature, initial_amount,light_intensity);
    
    			//For Invalid Input
    		default:
    			printf ("\n\nInvalid Input!!.\n");
    			printf ("Please enter another option.\n");
    			printf ("The option only consist of 1,2,3,4,5,6,7.\n\n");
    			system ("pause");
    			menu (temperature, initial_amount,light_intensity);
    			break;	
    		}
    }
    
    //Function Temperature
    int adjust_temperature (temperature_level)
    {
    	int new_temperature;
    
    		if(temperature_level == 1)
    		{
    				new_temperature = 5;
    		}
    		
    		else if(temperature_level == 2)
    		{
    				new_temperature = 10;
    		}
    		else if(temperature_level == 3)
    		{
    				new_temperature = 25;
    		}
    		else if(temperature_level == 4)
    		{
    				new_temperature = 30;
    		}
    
    		else
    		{
    				printf("\n\nInvalid Input!!.\n");
    				printf("Please enter another option.\n");
    				printf("The option only consist of 1,2,3,4.\n\n");
    				printf("Please reselect a temperature level: ");
    				scanf("%d",&temperature_level);
    		}
    	return new_temperature;
    }
    
    //Function Light Intensity
    int adjust_light_intensity (light_intensity_level)
    {
    	int light_intensity;
    
    	switch (light_intensity_level)
    	{
    	case 1:
    		light_intensity = 2500;
    		return light_intensity;
    		break;
    
    	case 2:
    		light_intensity = 5000;
    		return light_intensity;
    		break;
    
    	default:
    		printf("\n\nInvalid Input!!.\n");
    		printf("Please enter another option.\n");
    		printf("The option only consist of 1 and 2.\n\n");
    		printf("Please reselect a light intensity level: ");
    		scanf("%d",&light_intensity_level);
    	}
    }
    Now I encounter a problem... If I enter all the correct value the program can run correctly. But if I enter the wrong input for the temperature and light intensity, the value can be get and it will adjust the temperature and light intensity to 1.
    Here is the screenshot...
    Imageshack - 29447523.png

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    //Function Temperature
    int adjust_temperature (temperature_level)
    {
        int new_temperature;
    
        if(temperature_level == 1)
        {
        }
        ...
        else
        {
            ...
            scanf("%d",&temperature_level);
        }
        return new_temperature;
    }
    What is new_temperature if else is triggered, when this function returns?

    You also have the problem where if I enter an invalid value, it just asks me for a value again, and I can enter that wrong if I want too, and you don't do anything about that.

    You should really be compiling with warnings on.


    Quzah.
    kirthana likes this.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    okok thx for the advise i get it edi... ^.^

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Wink hei justin..i need ur help

    hi im studying in uniten.n i have the same question as u 4 my finalz asignment,,,...can u please help me? i really suck in c++ n i cant seem to get it ryte......can u email me the c++ codes to my email??? adamgiggs11@yahoo.co.uk

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kirthana View Post
    hi im studying in uniten.n i have the same question as u 4 my finalz asignment,,,...can u please help me? i really suck in c++ n i cant seem to get it ryte......can u email me the c++ codes to my email??? adamgiggs11@yahoo.co.uk
    1) If you have a problem, post your code here... nobody wants to email you.
    2) Could part of the problem be your lousy spelling and grammar?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  2. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  3. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:12 AM
  4. My menu program won't stop looping
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 03:30 PM
  5. My program won't stop looping
    By davie_scotland in forum C Programming
    Replies: 7
    Last Post: 01-18-2002, 01:34 PM

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