又是我,正在取得进展……我要感谢所有对我的上一个问题发表评论的人,这非常有帮助。到目前为止,我已经编译了它,但是有一些我无法解决的奇怪错误。
void addRecord(){//carries users input data
numRecs++;//increments numRecs by 1
struct record library; //this will hold info for user input
printf ("Please enter your first name:\n");
fgets(library.fName, sizeof(library.fName), stdin);
printf ("Please enter your last name:\n");
fgets(library.lName, sizeof(library.lName), stdin);
printf ("Please enter your hometown:\n");
fgets(library.hometown, sizeof(library.hometown), stdin);
printf("You entered %s for your first name.\n", library.fName);
printf("You entered %s for your last name.\n", library.lName);
printf("You entered %s for your hometown.\n", library.hometown);
struct record *myNewRecord;//creates a new struct pointer to store all the old data and new data
myNewRecord = malloc(numRecs * sizeof(struct record)); //allocates space to fit all old data plus the new struct data
if (myNewRecord == NULL)
{
fprintf(stderr,"Out of memory\n");
}
*myNewRecord = library;
fprintf(stderr, "You made it here!!\n");
这些是我从终端得到的结果。看起来源代码中的语法都是正确的,但问题是它出于某种原因跳过了名字 fgets。此外,当它打印出来时,它会以某种方式执行返回。大家能看出来怎么回事???PS当我消除开关盒并且只有 addrecord() 主要时,它不会这样做。
ubuntu@ubuntu:~$
ubuntu@ubuntu:~$ gcc lab222.c -o lab222
ubuntu@ubuntu:~$ ./lab222
Please select from the following:
1. Print all records.
2. Print number of records.
3. Print size of database.
4. Add record.
5. Delete record.
6. Print number of accesses to database.
7. Exit.
Enter a number 1-7:4
Please enter your first name:
Please enter your last name:
Don
Please enter your hometown:
Mega
You entered
for your first name.
You entered Don
for your last name.
You entered Mega
for your hometown.
You made it here!!