Thread: checking input from user

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    checking input from user

    I am doing this exercise where I have to draw a pyramid like below:
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********

    Here's the code I have, it does the job but it doesn't work well if user enters strings. It strangely goes on infinite loop. Why is that? I thought that scanf ignored strings so it would return 0 and it would fail condition but it's not working like what, what am I missing here?
    Also I'd like to know how you guys would code this. I have variable i map to number of rows and jto the the number of blank space to print.

    Code:
    
    #include <stdio.h>
    
    
    int main(void)
    {
        int height, i, j, k, readOkay;
        do
        {
            printf("Enter height(0 to 8): ");
            readOkay = scanf("%d", &height);
        }
        while ((height < 0) || height >8 || readOkay < 1);
        for (i = 0; i < height; i++)
        {
            j = (height + 1) - (i + 2);
            k=0;
            while (k < j)
            {
                printf(" ");
                k++;
            }
            while (k < (height + 1))
            {
                printf("*");
                k++;
            }
            printf("\n");
        } 
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If "scanf()" fails, whatever made it fail remains on the input buffer. Your code is going into an infinite loop because it keeps trying to read the same thing, failing, and looping again.

    If "scanf()" fails, you need to clear the bad data from the input buffer. An quick way is to use the "while getchar does not equal newline" loop if the input is invalid.

    A better idea is to use "fgets()" to read the input, and parse as needed (either with "sscanf()" or "strtol()").

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If the user enters only letters then readOkay will be < 1 and you loop back.
    But the letters are still sitting there in the input buffer!
    So scanf returns 0 yet again.
    Etc.
    You need to empty the input buffer before trying again.
    You could loop with getchar() until you get a newline.

    It's probably better to read the input a line at a time:
    Code:
    char line[512];
    int height = 0;
    do {
        fgets(line, sizeof line, stdin); // ignoring NULL return possibility
        if (sscanf(line, "%d", &height) != 1) {
            printf("Numbers, man, NUMBERS!!\n");
            continue;
        }
    } while (height < 0 || height > 8);

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    I tried to use strtol but it's not converting right, *ptr is poiting to 10 and not a '\0' or '\n' when i insert a valid number such as 8.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int height, i, j, k, readOkay;
        long int a;
        char aux[20], *ptr;
        do
        {
            printf("Enter height(0 to 8): ");
            fgets(aux, 20, stdin);
            a = strtol(aux, &ptr, 20);
            printf("%ld", a);
            printf("*ptr = %d", *ptr);
        }
        while ((a < 0) || a >8 || aux[0]=='0' || *ptr!='\n' || *ptr != '\0');
        for (i = 0; i < height; i++)
        {
            j = (a + 1) - (i + 2);
            k=0;
            while (k < j)
            {
                printf(" ");
                k++;
            }
            while (k < (a + 1))
            {
                printf("#");
                k++;
            }
            printf("\n");
        } 
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're making it more complicated than it needs to be. You don't need need the second parameter, so it can just be NULL (no *ptr necessary, and no need for the last 3 checks in the expression to "while").
    Note that the third parameter is the base (you probably want 10).

    If this is tricky, try the "sscanf()" method suggested by algorism and myself.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by telmo_d View Post
    I tried to use strtol but it's not converting right, *ptr is poiting to 10 and not a '\0' or '\n' when i insert a valid number such as 8.
    And what ASCII character has the value 10?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking User Input
    By Dusterdoo in forum C Programming
    Replies: 9
    Last Post: 06-25-2011, 03:04 PM
  2. Checking the user input.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 05-13-2008, 10:03 PM
  3. Checking user input?
    By hayai32 in forum C Programming
    Replies: 9
    Last Post: 04-03-2005, 05:33 PM
  4. Checking user input
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 08-05-2004, 10:32 AM
  5. Error checking user input
    By theharbinger in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 09:57 AM