Store Alphabet Only (仅存储字母)

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

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个字符时出现奇怪的符号。在这个程序下,我使用了一些重复的代码来处理数据结构。

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

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

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

答案1

得分: 2

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

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


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

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

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

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

  1. #include <ctype.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. ...
  7. char buf[BUFSIZ]; // BUFSIZ(来自<stdio.h>)至少为256。
  8. char name[20] = "";
  9. printf("插入姓名:");
  10. while (fgets(buf, sizeof buf, stdin)) {
  11. buf[strcspn(buf, "\n")] = 0; // 去掉可能的`\n`。
  12. // 长度检查
  13. size_t len = strlen(buf);
  14. if (len >= sizeof name) {
  15. printf("姓名太长!请插入姓名:");
  16. continue;
  17. }
  18. // 内容检查
  19. // 根据需要重新工作 - 也许接受空格?
  20. bool content_OK = len > 0;
  21. for (size_t i = 0; buf[i] && content_OK; i++) {
  22. unsigned char ch = buf[i];
  23. if (!isalpha(ch)) {
  24. content_OK = false;
  25. }
  26. }
  27. if (!content_OK) {
  28. printf("姓名中包含非字母字符!请插入姓名:");
  29. continue;
  30. }
  31. strcpy(name, buf);
  32. // 使用`name`。
  33. }

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

英文:

> 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.

  1. #include &lt;ctype.h&gt;
  2. #include &lt;stdbool.h&gt;
  3. #include &lt;stdio.h&gt;
  4. #include &lt;stdlib.h&gt;
  5. #include &lt;string.h&gt;
  6. ...
  7. char buf[BUFSIZ]; // BUFSIZ (from &lt;stdio.h&gt;) is at least 256.
  8. char name[20] = &quot;&quot;;
  9. printf(&quot;Insert Name: &quot;);
  10. while (fgets(buf, sizeof buf, stdin)) {
  11. buf[strcspn(buf, &quot;\n&quot;)] = 0; // Lop off potential `\n`.
  12. // Length check
  13. size_t len = strlen(buf);
  14. if (len &gt;= sizeof name) {
  15. printf(&quot;Name too long! Please insert name: &quot;);
  16. continue;
  17. }
  18. // Content check
  19. // Re-work as needed - maybe accept spaces?
  20. bool content_OK = len &gt; 0;
  21. for (size_t i = 0; buf[i] &amp;&amp; content_OK; i++) {
  22. unsigned char ch = buf[i];
  23. if (!isalpha(ch)) {
  24. content_OK = false;
  25. }
  26. }
  27. if (!content_OK) {
  28. printf(&quot;Name has non-letters! Please insert name: &quot;);
  29. continue;
  30. }
  31. strcpy(name, buf);
  32. // Use `name`.
  33. }

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:

确定