在 for 循环中使用结构的问题
这是一个家庭作业问题。我的编译器是CodeBlocks。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Address{
char number[5];
char street[30];
char city[30];
};
struct Employee{
char ID[7];
char name[31];
struct Address *addr;
};
int main(){
int n,i;
char temp[7];
printf("Enter number of Employee : ");
scanf("%d",&n);
struct Employee **p=(struct Employee **)malloc(n*sizeof(struct Employee *));
for (i=0; i<n; i++)
{
p[i]=(struct Employee *)malloc(sizeof(struct Employee));
p[i]->addr=(struct Address *)malloc(sizeof(struct Address));
}
for(i=0; i<n; i++)
{
printf("Employee #%d\n",i+1);
printf("Enter ID : ");
gets(p[i]->ID);
printf("Enter Name : ");
gets(p[i]->name);
printf("Enter Home number : ");
gets(p[i]->addr->number);
printf("Enter Street : ");
gets(p[i]->addr->street);
printf("Enter City : ");
gets(p[i]->addr->city);
}
}
我的问题是,当我运行此代码时,我无法输入 #1 员工的 ID;但是,我可以输入员工 #2 和 #3 的 ID。
我的问题出在哪里?
This is a homework question. My compiler is CodeBlocks.
Here is my code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Address{
char number[5];
char street[30];
char city[30];
};
struct Employee{
char ID[7];
char name[31];
struct Address *addr;
};
int main(){
int n,i;
char temp[7];
printf("Enter number of Employee : ");
scanf("%d",&n);
struct Employee **p=(struct Employee **)malloc(n*sizeof(struct Employee *));
for (i=0; i<n; i++)
{
p[i]=(struct Employee *)malloc(sizeof(struct Employee));
p[i]->addr=(struct Address *)malloc(sizeof(struct Address));
}
for(i=0; i<n; i++)
{
printf("Employee #%d\n",i+1);
printf("Enter ID : ");
gets(p[i]->ID);
printf("Enter Name : ");
gets(p[i]->name);
printf("Enter Home number : ");
gets(p[i]->addr->number);
printf("Enter Street : ");
gets(p[i]->addr->street);
printf("Enter City : ");
gets(p[i]->addr->city);
}
}
My problem is that when I run this code, I cannot enter the ID for the #1 employee; however, I can enter in the ID for employee #2 and #3.
Where is my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
gets() 在第一次循环之前从控制台读取某些内容似乎存在一些问题。
在循环之前添加
gets(temp);
似乎可以修复它。更好的解决方案是使用 gets() 之外的其他方法。There seems to be some issue with gets() reading something from the console before the first pass of the loop.
Adding
gets(temp);
right before the loop seems to fix it. A better solution would be to use something other than gets().初始的
scanf("%d", &n);
不会消耗尾随的换行符,因此它可用于gets()
调用。顺便说一句,永远不要使用
gets()
。它无法安全使用。例如,如果您正在读入 6 字节数组,而用户输入了 10 个字符,则会发生缓冲区溢出。考虑使用fgets()
代替(但请注意,与gets()
不同,它会将'\n'
字符保留在缓冲区中)。The initial
scanf("%d", &n);
doesn't consume the trailing newline, so it's left available for thegets()
call.Incidentally, never use
gets()
. It cannot be used safely. For example, if you're reading into a 6-byte array, and the user enters 10 characters, you have a buffer overflow. Consider usingfgets()
instead (but note that, unlikegets()
, it leaves the'\n'
character in the buffer).您应该在任何用户输入后显式清除输入缓冲区。并且您应该使用大小限制器确保您的输入安全。并且您应该使用 scanf 的返回值。
You should ever explicit clear the input buffer after any user-input. And you should make your inputs safe with size limiter. And you should use the return value from scanf.