【发布时间】:2016-01-21 18:12:54
【问题描述】:
我必须为我的班级编写代码。编码是关于要求用户输入他们的姓名、年龄和 ID。然后程序应该根据他们姓名中的前 6 个字母、他们的年龄和学生 ID 中的前两个字母来输入密码。问题是输出中的不明符号(╠╠╠╠╠╠╠╠╠╠╠╠╠╠)。谁能告诉我为什么会在那里>?然后它应该计算并显示密码的长度。代码如下:
#include <stdio.h>
#include <string.h>
void main(void)
{
char name[20], id[9], age[3], passcode[10];
int x;
puts("Enter your name:\n");
gets(name);
puts("\nEnter your student id:\n");
gets(id);
puts("\nEnter your age:\n");
gets(age);
x = strlen(passcode);
strncpy(passcode, name, 6);
strncat(passcode, id, 2);
printf("The passcode is:%s \n",passcode);
printf("The passcode has %d characters..\n",x);
}
它看起来像:
Enter your name:
johnny
Enter your student id:
dc87671
Enter your age:
20
The passcode is:johnny╠╠╠╠╠╠╠╠╠╠╠╠╠╠20dc
The passcode has 22 characters..
Press any key to continue . . .
【问题讨论】:
-
您不应该使用 get 从标准输入读取字符串。使用 fgets() 或 getline() 等函数