0

I'm writing a program to validate a username that's entered by the user. For the purposes of this project, we're to allow alphabetic letters (upper or lowercase), digits or underscores, but no spaces or other punctuation. It also has to be between 5 and 10 characters total. I believe my issue is with getchar() since I know it can only hold one character at a time, but I'm not entirely sure the best way to fix it. Currently when I run my code it only comes back as invalid. Do I need to change my loop or make adjustments to it? Or is there an issue with my if statement?

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

int main(void)
{


    int ch;
    int len = 0;


    printf("Enter the username: "); //prompt user to enter a username
    ch = getchar();


    while (ch != '\n') //while loop checking for length of username
    {
        len++;
        ch = getchar();
    }

    if(isspace(ch) || ispunct(ch) || len > 10 || len < 5){

            printf("invalid input.");
    }

    else{
    printf("valid input.");
    }

    return 0;

}

1 Answers1

1

The problem is on this function: isspace(ch). It returns a non-zero value(true) if the character is a white-space. Standard white-spaces are

' '   (0x20)    space (SPC)
'\t'    (0x09)  horizontal tab (TAB)
'\n'    (0x0a)  newline (LF)
'\v'    (0x0b)  vertical tab (VT)
'\f'    (0x0c)  feed (FF)
'\r'    (0x0d)  carriage return (CR)

Since the last action you take is pressing enter, the last char will be a newline or carriage return depending on the OS('\r\n', '\n' or '\r').

I believe you intended to check if the name has a space in-between the characters. The way you're doing it, you only check the last one. You could add all chars to a buffer and check it later, or change your initial while condition to check for invalid chars.

EDIT Since it seems you're still having trouble from the comments, I decided to add a possible solution here:

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

int main(void)
{
    int ch;
    int len = 0;

    printf("Enter the username: "); //prompt user to enter a username
    ch = getchar();


    while (!isspace(ch) && !ispunct(ch)) //while loop checking for length of username. While it's not(note the exclamation mark) a whitespace, or punctuation, it keeps going(newline is considered a whitespace, so it's covered by the loop).
    {
        len++;
        ch = getchar();
    }

    if (ch == '\n' && len <= 10 && len >= 5) {//if it found the newline char(considering the newline is \n), it means it went till the end without finding other whitespace or punctuation. If the lenght is also correct,then the username is valid
      printf("valid input.");
    }
    else {//if the loop stopped because it found a space or puncuation, or if the length is not correct, then the input is invalid
      printf("invalid input.");
    }

    return 0;
}
savram
  • 500
  • 4
  • 18
  • Ok. I thought the issue might be i'm only checking the final character, so I'm glad to hear I'm on the right track at least. What would I need to change about my while loop? I was trying to add the 'isspace(ch) || ispunct(ch)' to the condition, But then I realized it wouldn't bother with the while loop at all since it's not meeting the conditions. –  Sep 09 '17 at 23:48
  • Ok so I removed the isspace(ch) function completely and it seems to be working (even detecting spaces..although I'm not sure how. Maybe because I added != EOF? Anyway I left the ispunct(ch) in and it's still accepted punctuation...is this the same issue how it's only checking the last character? –  Sep 09 '17 at 23:59
  • Ok I edited the answer with a possible solution, hope it helps – savram Sep 10 '17 at 00:22
  • Thank you! I now see the issue was with my while statement, not the if else statement...Is there a function that can exempt underscores? Because those are supposed to be valid. All other punctuation are invalid. –  Sep 10 '17 at 00:27
  • I'm not aware of such a function, but you should easily be able to change the code to accept underscores with a simple 'or' in the while loop. Glad I could help ;) – savram Sep 10 '17 at 00:37