0

My windows works slowly, so I installed a standalone Ubuntu 32-bit.

here's my code:

/**
* mario.c
* 
* Kiril Reznikovsky
* [email protected]
* Builds a half-piramid according to given height
*/

#include <stdio.h>
#include <ctype.h>

int main()
{
    int height;

    printf("Please insert zero or a positive integer no greater than 23: ");    
    scanf("%d",&height);

    while((isdigit(height) == 0) || height > 23 || height < 0)
    {
        printf("Please insert zero or a positive integer no greater than 23: ");    
        scanf("%d",&height);
    }

    printf("\n");

    for(int pLvl = 0;pLvl < height;pLvl++)
    {
        for(int blanks = pLvl;blanks < 7;blanks++)
        {
            printf(" ");
        }
        for(int bricks = 0;bricks < pLvl + 2;bricks++)
        {
            printf("#");
        }
        printf("\n");
    }

    return (0);
}

I don't understand why isdigit is not doing what it is supposed to do.

1 Answer 1

1

Per the isdigit()'s man page we see

These functions check whether c, which must have the value of an unsigned char or EOF, falls into a certain character class according to the current locale.

So isdigit() gets a char as input. Not an int as you use it. It also checks for only one-digit numbers, for example the char '5' will return a non-zero value, but the array of chars (a string) like "10" will give you an error. Better use the included function GetInt() that makes sure the user inputs an int and then check this int yourself to be in the space you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .