Thread: nested while loop help

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    Exclamation nested while loop help

    I'm creating a program that will test a range of temperatures for validity. I have become stuck, in this case am trying to print a message saying invalid input when a user enters a value not in the range of 425 - 435.

    Code:
    #include <stdio.h>
    #define sentinel 999
    
    int 
    main(void)
    {
        int temp;
        double valid;
    
    
        printf ("Enter the number 999 when you are finished entering numbers.\n\n");
        printf ("Enter the temperature range :", sentinel);
        scanf("%d", &temp);
    
    
        while (temp != sentinel ) {
               valid = 1;
        while (valid) {
                if (temp > 425 && temp < 435)
                    valid = 0;
        }
            printf("The product cure temperature is good!\n");
            break;
        }
    
    
        return (0);
    }
    Last edited by Oo Kami; 02-04-2012 at 03:32 AM. Reason: left out some important code

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put your printf, and scanf() for temp, inside the first while loop (and before the second while loop).

    I wouldn't use the variable "valid" for testing, in both while loops.
    Code:
    if(temp == sentinel)
       break;
    in the outer while loop seems sufficient. That reduces the outer while loop to just while(1), with the printf()'s, and the scanf() for temps, and with the above if(test==sentinel), test in it.

    Keep valid for the nested while loop, and keep it simple. Not really needed here, but don't be afraid in C, to use a small function for things like a nested loop. Small and simple functions make getting the logic right, *much* easier, and easier to alter, etc., when you need to.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why would you make a simple flag variable a DOUBLE? Taking up eight bytes of memory and then messing with floating point values just for a single 1 or 0 value?

    Instead, why not:
    Code:
    while (temp > 425 && temp < 435) {
        scanf("%d", temp);
        if (temp == sentinel) break;
    }
    Or something like this, you can add the print statements.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Thanks guys ^^ i understand it better now. ill try and see if i can get the hang of this am still pretty new to C. ill come back if i still dont get it :P

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Sorry to bother again. i changed the code a bit thanks to your advice. i just can't figure out how to make it print input is invalid.
    can you please show a fixed version of my code and explain what you did? am still a noob at c.

    Code:
    int main(void)
    {
        int temp;
        int testing;
    
    
    
    
        printf ("Enter the number 999 when you are finished entering numbers.\n\n");
        printf ("Enter the temperature range:", sentinel);
        scanf("%d", &temp);
        
        if (temp < 425 && temp > 435) {
            printf("your input is invalid range is 425 - 435");
        }
    
    
        while (temp != sentinel ) {
            testing = 1;
        while (testing) {
                if (temp >= 425 && temp <= 435)
                    testing = 0;
        }
            printf("The product cure temperature is good!\n");
            break;
        }
    
    
    
    
        return (0);
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Oo Kami View Post
    Sorry to bother again. i changed the code a bit thanks to your advice. i just can't figure out how to make it print input is invalid.
    can you please show a fixed version of my code and explain what you did? am still a noob at c.

    Code:
    int main(void)
    {
        int temp;
        int testing;
    
    
    
    
        printf ("Enter the number 999 when you are finished entering numbers.\n\n");
        printf ("Enter the temperature range:", sentinel);
        scanf("%d", &temp);
        
        if (temp < 425 && temp > 435) {
            printf("your input is invalid range is 425 - 435");
        }
    ....
    }
    What exactly is your condition asking and is it what you really want to check? Take a look at If Statements, specifically look at the boolean operators AND (&&) and OR(||).
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Thanks you just taught me || means or with that i was finally able to print a message if a user enters values not in the range of 425 - 435.
    Code:
    	while (temp != sentinel ) {
    		if(temp<425 || temp>435) {
    			printf("Warning! AT risk!\n");
    			exit(0);
    		}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  2. Nested Loop
    By DuckCowMooQuack in forum C Programming
    Replies: 8
    Last Post: 02-15-2011, 10:37 AM
  3. nested for loop
    By aromash in forum C Programming
    Replies: 4
    Last Post: 10-25-2010, 02:41 AM
  4. nested for loop
    By altf4thc in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2010, 12:43 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM