英文:
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(" %[^\n]s", 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. Usefgets()
to read a line of user input. Usestrcspn()
to lop off a trailing'\n'
. -
Separate user input from validation.
-
Use
isalpha()
to detect letters. Useisdigit()
to detect digits. -
Use various tests on input to assess its acceptability. See Name validation best practices.
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
...
char buf[BUFSIZ]; // BUFSIZ (from <stdio.h>) is at least 256.
char name[20] = "";
printf("Insert Name: ");
while (fgets(buf, sizeof buf, stdin)) {
buf[strcspn(buf, "\n")] = 0; // Lop off potential `\n`.
// Length check
size_t len = strlen(buf);
if (len >= sizeof name) {
printf("Name too long! Please insert name: ");
continue;
}
// Content check
// Re-work as needed - maybe accept spaces?
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("Name has non-letters! Please insert name: ");
continue;
}
strcpy(name, buf);
// Use `name`.
}
I would put this code to read a name into a helper function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论