Like Tree1Likes
  • 1 Post By anduril462

user input bounds not working

This is a discussion on user input bounds not working within the C Programming forums, part of the General Programming Boards category; Hello, I am prompting for user input and I want the program to ONLY execute if the input is an ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    user input bounds not working

    Hello, I am prompting for user input and I want the program to ONLY execute if the input is an integer between 1 and 23 and if its not repeat the prompt. However if I give an integer below 1 the function stops and does not execute and if I give an integer above 23 the function still executes.

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    
    int main(void)
    {
        int height;
        // prompts for user input & checks for correctness
        do
        {
            printf("Height: ");
            height = GetInt();
        }
        while (height<=0 && height>23);
        
        // handles the line variable
        for(int line=1;line<=height;line++)
            {   
                 // calculates number of spaces
                 for(int space=1;space<=(height-line);space++)
                {
                    printf(" ");
                }
                 // calculates number of hashes
                for(int hash=1;hash<=(line+1);hash++)
                {
                    printf("#");
                }
            // starts new line    
            printf("\n");
            }
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Code:
    while (height<=0 && height>23);
    Read that carefully, perhaps out loud to yourself. Tell me if that makes sense, or is even possible. If you need to, draw each half of that on a number line, and see if both conditions can be true at the same time (&&). Then, think about the operator you are using to cover both error conditions.
    intex likes this.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Hello again, I changed it to || (or) I appreciate the prompt reply. Thanks a lot! I guess it didn't make sense when I went through it out loud.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 09-25-2011, 06:49 AM
  2. End of user input must be 999
    By naspek in forum C Programming
    Replies: 23
    Last Post: 07-22-2009, 11:04 PM
  3. "User Input" not working as intended
    By daedenilus in forum C Programming
    Replies: 3
    Last Post: 11-13-2005, 04:34 PM
  4. User-input not working
    By Serejai in forum C++ Programming
    Replies: 6
    Last Post: 08-22-2004, 06:55 PM
  5. user input
    By hen in forum C Programming
    Replies: 16
    Last Post: 06-27-2002, 11:09 PM

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