Store Alphabet Only (仅存储字母)

huangapple go评论55阅读模式
英文:

Store Alphabet Only

问题

I have data declared as a char name[20], but my program can read numbers when the user key in the input. A message should appear if not valid or does not have enough space. Got a way to solve my problem?

我的数据声明为char name[20],但是当用户输入时,我的程序可以读取数字。如果输入无效或没有足够的空间,应该显示一条消息。有解决方法吗?

My code also reads more than 20 characters and appears weird symbols when characters of more than 20. I use some repetition code under this program for data structure

我的代码还读取了超过20个字符,并在超过20个字符时出现奇怪的符号。在这个程序下,我使用了一些重复的代码来处理数据结构。

printf("Insert Name: ");
while (scanf(" %[^\n]s", name) != 1) {
  printf("Name you insert not valid! Please insert name: ");
  while (getchar() != '\n');
}
英文:

I have data declared as a char name[20], but my program can read numbers when the user key in the input. A message should appear if not valid or does not have enough space. Got a way to solve my problem?

My code also reads more than 20 characters and appears weird symbols when characters of more than 20. I use some repetition code under this program for data structure

printf("Insert Name: ");
while (scanf(" %[^\n]s", name) != 1) {
  printf("Name you insert not valid! Please insert name: ");
  while (getchar() != '\n');
}

答案1

得分: 2

> 我的代码还读取了超过20个字符,并显示了奇怪的符号。

scanf("%[^\n]s", name)不能防止将太多字符读入name。这会导致_未定义行为_(UB)和各种问题。请查阅scanf()文档以了解_width_和正确的格式说明符。


  • 在了解为什么不好之前停止使用scanf()。使用fgets()来读取用户输入的一行。使用strcspn()来去掉结尾的'\n'

  • 将用户输入与验证分开。

  • 使用isalpha()检测字母。使用isdigit()检测数字。

  • 对输入进行各种测试以评估其可接受性。参见名称验证最佳实践

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
    
...
    char buf[BUFSIZ];  // BUFSIZ(来自<stdio.h>)至少为256。
    char name[20] = "";
    printf("插入姓名:");
    while (fgets(buf, sizeof buf, stdin)) {
      buf[strcspn(buf, "\n")] = 0; // 去掉可能的`\n`。

      // 长度检查
      size_t len = strlen(buf);
      if (len >= sizeof name) {
        printf("姓名太长!请插入姓名:");
        continue;
      }
        
      // 内容检查
      // 根据需要重新工作 - 也许接受空格?
      bool content_OK = len > 0;
      for (size_t i = 0; buf[i] && content_OK; i++) {
        unsigned char ch = buf[i];
        if (!isalpha(ch)) {
          content_OK = false;
        }
      }
      if (!content_OK) {
        printf("姓名中包含非字母字符!请插入姓名:");
        continue;
      }

      strcpy(name, buf);
      // 使用`name`。
    }

我会将这段代码放入一个辅助函数来读取一个_姓名_。

英文:

> My code also reads more than 20 characters and appears weird symbols

scanf(&quot; %[^\n]s&quot;, name) does not prevent too many characters from being read into name. This leads to undefined behavior (UB) and all sorts of troubles. Review scanf() documentation for width and proper specifiers.


  • Stop using scanf() until you know why it is bad. Use fgets() to read a line of user input. Use strcspn() to lop off a trailing &#39;\n&#39;.

  • Separate user input from validation.

  • Use isalpha() to detect letters. Use isdigit() to detect digits.

  • Use various tests on input to assess its acceptability. See Name validation best practices.

#include &lt;ctype.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
    
...
    char buf[BUFSIZ];  // BUFSIZ (from &lt;stdio.h&gt;) is at least 256.
    char name[20] = &quot;&quot;;
    printf(&quot;Insert Name: &quot;);
    while (fgets(buf, sizeof buf, stdin)) {
      buf[strcspn(buf, &quot;\n&quot;)] = 0; // Lop off potential `\n`.

      // Length check
      size_t len = strlen(buf);
      if (len &gt;= sizeof name) {
        printf(&quot;Name too long! Please insert name: &quot;);
        continue;
      }
        
      // Content check
      // Re-work as needed - maybe accept spaces?
      bool content_OK = len &gt; 0;
      for (size_t i = 0; buf[i] &amp;&amp; content_OK; i++) {
        unsigned char ch = buf[i];
        if (!isalpha(ch)) {
          content_OK = false;
        }
      }
      if (!content_OK) {
        printf(&quot;Name has non-letters! Please insert name: &quot;);
        continue;
      }

      strcpy(name, buf);
      // Use `name`. 
    }

I would put this code to read a name into a helper function.

huangapple
  • 本文由 发表于 2023年6月26日 23:05:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定