Like Tree1Likes
  • 1 Post By grumpy

Limiting input to only positive integers?

This is a discussion on Limiting input to only positive integers? within the C Programming forums, part of the General Programming Boards category; I'm writing the Mario half pyramid and I'm using user input to create a pyramid of the height (user input). ...

  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
    289
    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
    2,610
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    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.
    laserlight likes this.
    Right 98% of the time, and don't care about the other 3%.

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, 04:11 PM
  5. Positive integers
    By cxs00u in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2002, 05:11 PM

Tags for this Thread


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