n00b question regarding loop termination

This is a discussion on n00b question regarding loop termination within the C Programming forums, part of the General Programming Boards category; Hi, I'm a beginner, please be gentle! I have created a very simple program which reads in the dimensions of ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    n00b question regarding loop termination

    Hi,

    I'm a beginner, please be gentle!

    I have created a very simple program which reads in the dimensions of a rectangle then calculates area and volume.

    The loop will terminate if a non-numerical integer is entered - is there a way of making it terminate if an integer which is equal to or less than 0 is entered? Ideally I'd like to make it so the loop will terminate if 0 or a negative number is entered at any of the three scanf() stages (enter length, enter width, enter height).

    Here's my code:

    Code:
    #include <stdio.h>
    int main(void)
    {
        int length, width, height;
        
        printf("Enter the length of the rectangle: ");
        while (scanf("%d", &length) == 1)
        {
           printf("Length = %d:\n", length);
           printf("Enter the width: ");
           if (scanf("%d", &width) !=1)
              break;
           printf("Enter the height: ");
           if (scanf("%d", &height) !=1)
              break;    
           printf("The rectangle's area is %d and its volume is %d.\n", width * height, (length * width) * height);
           printf("Enter the length of the rectangle: ");
        }
        
        printf("Invalid - BYE.\n");
    
        getchar();
        getchar();
        getchar();
        return 0;
        
    }
    Any advice/guidance gratefully received

    Cheers,

    Twazzler

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    if( width <= 0 ) {
    
    break;
    }
    Duh~ Did you even read your text book?
    Edit: added break;
    Last edited by Bayint Naung; 04-08-2011 at 02:30 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Did I even read my text book?!

    Yes I did, until the words became a painful blur...it wasn't very helpful, hence my posting the question here.

    Very kind of you to reply, but where in the code would I add that without disrupting the existing entries?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Code:
    for (;;) { /* forever */
        printf("Enter the length of the rectangle: ");
        scanf("%d", &length);
        if (length <= 0)
            break;
    
        printf("Enter the width: ");
        scanf("%d", &width);
        if (width <= 0)
            break;
    
        printf("Enter the height: ");
        scanf("%d", &height);
        if (height <= 0)
            break;
    
        printf("The rectangle's area is %d and its volume is %d.\n", width * height, (length * width) * height);
        }
    ...
    bye bye stuff

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    Are you mad nonoob? You have written an infinite loop unless any non-positive value is used.
    Use condition to stop the scanf function only after 1 valid input has been taken for each of the required values.
    Twazzler you should make the program better so as to show an error message and ask for the value again.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Quote Originally Posted by Abhas View Post
    Are you mad nonoob? You have written an infinite loop unless any non-positive value is used.
    It's what was asked for.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    oh sorry i misunderstood the question.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Thank you - so was there no way of doing what I wanted to do with a while loop? It would have to be for?

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Since you want to terminate when ANY of the three entered values is <=0, then any loop would look similar. Even a while.
    I suppose you could do while (1) { ... } which means while always true.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Thanks again.

    I have also written some code which replaces the vowels of an inputted sentence with 'Z'. If I wanted to remove the whitespaces from the outputted sentence, what would I put below if (ch == ' ') to remove it?
    Here's what I have so far:
    Code:
    #include <stdio.h>
        int main(void)
        {
            char ch;
            
            while ((ch = getchar()) != '.')
            {
                  if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 
                  putchar('Z');
                  else
                      putchar(ch);
                  
            }
            putchar(ch);
            
            getchar();
            return 0;
            
            }

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Code:
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 
        putchar('Z');
    else if (ch == ' ')
        /* do nothing */ ; <-- yes you need this semicolon
    else
        putchar(ch);

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    nonoob, you are a gentleman and a scholar

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newb loop question
    By filio623 in forum C++ Programming
    Replies: 10
    Last Post: 09-18-2009, 03:54 PM
  2. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 09:39 AM
  3. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-30-2008, 12:12 AM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. for loop question
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 03-20-2006, 09:42 AM

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