-2

Please tell me why my else if does not work? I'm trying to check if the input value is correct or not. If it is not isalpha() or isdigit() it will out put an error! Everything works okay except the else if! Thank you!

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

int main(void)
{
    char name;
    int len = 0;

    printf("Enter the user name: ");
    name = getchar();

    while (name != '\n')
    {

        name = getchar();
        int i;
        for (i = 0; i <= (sizeof(name)/2); i ++)
        {
            len++; 
        }
    }

    printf("len = %d\n", len);

    if((len < 5) || (len > 10 ))
    {
        printf("Output: input is invalid"); 
    }
    else if((isdigit(name)) || (isalpha(name))) //this one does not work
    {
        printf("invalid");
    }
    else
    {
        printf("Output: input is invalid");
        return 0;
    }   

    return (0); 
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Man Dang
  • 21
  • 1
  • 6
  • 2
    because according to your `while` statement your 'name' after finishing the loop is `\n` which is neither a digit nor a letter. it is a 'space'. – Serge Sep 08 '17 at 02:05
  • so, should I change to do-while? – Man Dang Sep 08 '17 at 02:07
  • You need check in while-loop. Also digits is not alphabets. So I do not think it is necessary to check by isdigit. – BLUEPIXY Sep 08 '17 at 02:09
  • 1
    @ManDang changing the loop type will not help you. you will need to change the algorithm in accordance to what you want to do. – Serge Sep 08 '17 at 02:14
  • ok, I'm changging @Serge – Man Dang Sep 08 '17 at 02:16
  • If 'name' is declared as type char, then sizeof(name) is one. Your loop body will be executed once and only once, since sizeof(name)/2 is zero. Your loop then breaks down to the simple statement 'len++;' – FredK Sep 08 '17 at 03:45
  • Compile with all warnings and debug info (`gcc -Wall -Wextra -g`) then use the debugger (`gdb`) e.g. to run your program step by step to understand what is wrong. – Basile Starynkevitch Sep 08 '17 at 04:41
  • 1
    I'm not even sure what the intent of this program is. – Michael Burr Sep 08 '17 at 04:43
  • Possible duplicate of [isdigit raises a debug assertion when entering £ and ¬](https://stackoverflow.com/questions/28077229/isdigit-raises-a-debug-assertion-when-entering-%c2%a3-and-%c2%ac) – autistic Sep 08 '17 at 05:05
  • Note that I voted as a duplicate to *guide you away from the erroneous belief that `getchar` should be stored into a `char`*... Tell me, which [`getchar` manual](http://pubs.opengroup.org/onlinepubs/009695399/functions/getchar.html) are you reading which suggests that's a good idea? Or are you just guessing? Because you *can't* hope to learn to write portable C code by *just guessing*. – autistic Sep 08 '17 at 05:08
  • We've answered questions almost exactly like this too many times. Did you ever think to read the "help center" and/or do some searches for already existing [`isdigit`](https://stackoverflow.com/search?q=%5Bc%5D+isdigit), [`islower`](https://stackoverflow.com/search?q=%5Bc%5D+islower), etc so on and so forth questions? Because I bet if you did, you'd have found your answer quite quickly! – autistic Sep 08 '17 at 05:12
  • 1
    What exactly are you trying to do with this program? Are you trying to store a string in `name`? – J...S Sep 08 '17 at 05:32

1 Answers1

0

Since the message printed before reading value in name is Enter the username and as usernames are usually strings, I suppose you are reading a string and not a single character.

If that's the case, change

char name;

to

char name[20]; //20 is just a number. Make it bigger if you need

because a string is just a sequence of characters which may be stored in a character array.

You could use fgets() to read into name.

fgets(name, sizeof(name), stdin);

fgets will stop reading when a \n is encountered but the \n will be stored in name as well.

So replace the \n with \0 to mark the end of string.

name[strlen(name)-1]='\0';

For finding the length of the string with strlen() for storing into len.

else if((isdigit(name)) || (isalpha(name))) //this one does not work
{
    printf("invalid");
}

If you are checking the last character entered into name in the else if, it may be re-written as

else if(isalnum(name[len-1]))
{
    printf("invalid");
}

isalnum() will return non-zero if its argument is alphanumeric.




Now in case you really did mean name to be a single character and just need the last entered character for comparison in the else if.

If name is a char, sizeof(char) must be 1. So

for (i = 0; i <= (sizeof(name)/2); i ++)
{
    len++; 
}

is equivalent to just

len++;

as sizeof(name)/2 is 0.

Note that getchar() returns an int and not a char and the value returned is EOF if some error occurred.

Try

while (name != '\n' && name != EOF)
J...S
  • 5,079
  • 1
  • 20
  • 35