0

How do I make an error message if only enter or a space is entered?

    do
    {
        printf("Please enter a username: ");
        scanf("%s", name);
        if(name[0] == '\n')
        {
            printf("invalid input");
        }
        printf("> %s\n", name);

    }
    while(strlen(name) < 2 || strlen(name) > 15);
Clifford
  • 88,407
  • 13
  • 85
  • 165

2 Answers2

1

The newline will result in an empty string so the format specifier is not satisfied, so scanf() does not return. It is somewhat arcane behaviour, but:

int count = scanf( "%16[^ \n]s", name ) ;

will return when newline or leading whitespace is entered with count == 0. It will also accept no more than 16 characters, preventing a buffer overrun while allowing the > 15 characters test.

To avoid unnecessary tests and multiple calls to strlen() for the same string:

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

int main()
{
    char name[20] = "" ;
    bool name_valid = false ;
    while( !name_valid )
    {
        printf("Please enter a username: ");
        int count = scanf("%16[^ \n]", name);

        size_t len = count == 0 ? 0 : strlen( name ) ;
        name_valid = len > 1 && len < 16 ;

        if( !name_valid )
        {
            int ch ;
            do
            {
                ch = getchar() ;
            } while( ch != '\n' && ch != EOF ) ;

            printf("invalid input\n");
        }
    }
    printf("> %s\n", name);

    return 0;
}

Note that in "invalid input" you need to remove the buffered newline and any preceding junk.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

I personally don't like using scanf() b/c it results in a lot of trouble and it is not safe to use it in a production environment. I recommend using fgets() instead, and here is how it works:

#include <stdio.h>
#include <string.h>

int main()
{
    char buffer[50];
    int buffer_size = sizeof(buffer);

    /* accepts string input from user */
    printf("Please enter a username: ");
    fgets(buffer, buffer_size, stdin);

    /* then remove "\n" from the string 
    A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
    */
    buffer[strcspn(buffer, "\n")] = 0;

    /* check if user has entered an empty string */
    /* strcmp(): compares two strings and returns 0 if comparision is true */
    if ((strcmp(buffer, "")) == 0 || (strcmp(buffer, " ")) == 0)
        printf("I can detect that you printed nothing! \n");
    else
        printf("You printed: %s", buffer);

    return 0;
}

It may seem like an absurd amount of work to do just to get input from user, but this is how it works in C.

Zaki
  • 107
  • 9
  • What if the user enters multiple spaces or a TAB? Calling strcmp to test for an empty or single character string _is_ perhaps not an "absurd" but certainly unnecessary amount of work. – Clifford Mar 15 '21 at 07:43
  • @Clifford You are right, strcmp is unnecessary and there is definitely room for improvement in this code, maybe he can use atol instead? My point is that fgets() approach is better than scanf() especially for beginners. – Zaki Mar 15 '21 at 12:24
  • Fair enough - the main point being to use `fgets()` is not in dispute - `scanf()` is arcane and confusing. I am not sure how `atol()` offers a solution - this is not numeric input. I merely intended : `if( buffer[0] == '\n' || (buffer[0] == ' ' && buffer[1] = '\0') )` as equivalent to your `strcmp()` expression. But my point about this only accepting a single space as invalid stands. I would assume that "space" in the question refers to any kind and any length of whitespace - since that would be the behaviour of the original code. – Clifford Mar 15 '21 at 12:45