【问题标题】:creating username function what if there is no input at all just pressing enter?创建用户名函数如果根本没有输入怎么办?
【发布时间】:2021-06-12 02:04:40
【问题描述】:

如果只输入或输入空格,如何提示错误信息?

    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);

【问题讨论】:

  • scanf("%s", name);不能输入包含空格的字符串,或者空字符串。
  • 我可以用什么来代替这个?
  • 如果您从fgets() 开始,您可能会发现数据输入更容易,请记住保留任何换行符(如果字符串中有空间)。
  • fgets 对我不起作用,或者我之前在代码中使用了 scanf,也许这就是为什么?
  • 请不要混用您的输入法。请参阅fgets doesn't work after scanf 及其自己的重复链接。

标签: c input scanf do-while


【解决方案1】:

换行符将产生一个空字符串,因此不满足格式说明符,因此scanf() 不会返回。这有点神秘,但是:

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

将在使用count == 0 输入换行符或前导空格时返回。它还将接受不超过 16 个字符,防止缓冲区溢出,同时允许 > 15 个字符测试。

为避免对同一字符串进行不必要的测试和多次调用strlen()

#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;
}

请注意,在“无效输入”中,您需要删除缓冲的换行符和任何前面的垃圾。

【讨论】:

    【解决方案2】:

    我个人不喜欢使用 scanf() b/c 它会带来很多麻烦,而且在生产环境中使用它也不安全。我建议改用 fgets(),它是这样工作的:

    #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;
    }
    

    仅仅从用户那里获取输入似乎是一项荒谬的工作,但这就是它在 C 中的工作方式。

    【讨论】:

    • 如果用户输入多个空格或一个TAB怎么办?调用 strcmp 来测试空字符串或单个字符串 可能不是“荒谬”,但肯定是不必要的工作量。
    • @Clifford 你说得对,strcmp 是不必要的,这段代码肯定有改进的空间,也许他可以用 atol 代替?我的观点是 fgets() 方法比 scanf() 更好,尤其是对于初学者。
    • 很公平 - 使用 fgets() 的要点没有争议 - scanf() 是神秘而令人困惑的。我不确定atol() 是如何提供解决方案的——这不是数字输入。我只是打算:if( buffer[0] == '\n' || (buffer[0] == ' ' &amp;&amp; buffer[1] = '\0') ) 等同于您的 strcmp() 表达式。但我的观点只接受一个空格作为无效的立场。我会假设问题中的“空格”指的是任何类型和任何长度的空格 - 因为那将是原始代码的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    相关资源
    最近更新 更多