Thread: Limiting input to only positive integers?

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

    Question Limiting input to only positive integers?

    I'm writing the Mario half pyramid and I'm using user input to create a pyramid of the height (user input). However, I forgot to limit it to only positive integers (1-23) and not non integers or just pressing "enter". Any ideas on how I can go about doing this?

    Code:
    #include <stdio.h>
    
    
    int main(int argc, const char *argv[])
    {
        int height = 0;
        int i=0,j=0,k=0;
        
        do 
        {
            
            printf("Height: ");
            scanf("%d", &height);
        }
        while (height < 0 || height > 23); 
        
        
    
    
        int spacing=height-1;
        int hash=2;
        
        for (i=0; i<height; i++)
            {
                for (j=spacing; j>0; j--)
                {
                    printf(" ");
                }
                for (k=0; k<hash; k++)
                {
                    printf("#");
                }
                spacing--;
                hash++;
                printf("\n");
            }
            
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    easiest way is to use either a do..while loop, or just a if..else after the input. i believe this is called input validation, or validating inputs

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The beginning's way is to set "height" to a wrong/invalid value inside the do loop before the scanf function call.

    The more advanced way is to use the value returned by the scanf function in the while condition.
    http://www.cplusplus.com/reference/cstdio/scanf/

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no way to stop the user from entering invalid characters, or just hitting the enter key (although the latter makes no difference with scanf()).

    The more usual technique, if you want your program to be able to eat/ignore invalid input without getting into an infinite loop, is to use fgets() to read each line, and then check the content of the line before trying to scan an integer.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-01-2012, 08:00 AM
  2. Replies: 2
    Last Post: 09-25-2012, 10:54 PM
  3. find the totals of positive and negative integers
    By noaksey in forum C Programming
    Replies: 5
    Last Post: 05-11-2004, 01:59 PM
  4. Replies: 6
    Last Post: 11-04-2002, 05:11 PM
  5. Positive integers
    By cxs00u in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2002, 06:11 PM

Tags for this Thread