我目前在课堂上的项目是制作一个使用结构充当电话簿的程序。我已经对“添加联系人”功能和“显示联系人”功能进行了编码,但是当我有多个联系人时,“显示联系人”功能显示了最近联系人的所有期望。有人可以帮我找到问题吗?
这是我当前的代码。如果有任何令人困惑的地方,请告诉我,我将尽力尽快做出回应。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct contact { // Data structure that holds contact information
char FirstName[10]; // Array for first name
char LastName[10]; // Array for last name
int PhoneNum; // Phone number
};
int main (){
// Setting up variables
int Function = 0;
int Choice = 0;
char FName[200][10];
char LName[200][10];
int PNum = 0;
int n = 1;
struct contact *con[200];
int b = 0;
do{ // Will loop through the main function until the user decides to exit the program
// Prints out the main menu of the phone book
printf("\nPhone Book");
printf("\n[1] Add a contact");
printf("\n[2] Delete a contact");
printf("\n[3] Show contacts");
printf("\n[4] Exit program");
printf("\n\nWhat function would you like to use?\n"); // Asks for user input
scanf("%d", &Choice);
switch(Choice){
case 1: // Case to add a contact into the phone book
printf("\nYou chose to add a contact.");
printf("\nFirst name: ");
scanf("%s", &FName[b]);
printf("\nLast name: ");
scanf("%s", &LName[b]);
printf("\nPhone number (Numbers only): ");
scanf("%d", &PNum);
printf("\nRecord added to the phone book");
// Records the information given into the structure
struct contact *con = (struct contact*)malloc(n*sizeof(struct contact));
int a = (n - 1);
printf("\na is %d", a);
strcpy(con[a].FirstName, FName[b]);
strcpy(con[a].LastName, LName[b]);
con[a].PhoneNum = PNum;
int b = (b+1);
n++;
printf("\nn is %d", n);
// Prints out the given information
printf("\nNew contact:");
printf("\nFirst name: %s", con[a].FirstName);
printf("\nLast name: %s", con[a].LastName);
printf("\nPhone number: %d", con[a].PhoneNum);
printf("\n");
break;
case 2: // Case to delete a contact from the phone book
printf("\nYou chose to delete a contact.");
break;
case 3: // Case to see all of the entered contacts
printf("\nYou chose to show the contacts.");
for(a = 0; a < (n - 1); a++){
printf("\nContact #%d", a);
printf("\nFirst name: %s", con[a].FirstName);
printf("\nLast name: %s", con[a].LastName);
printf("\nPhone number: %d", con[a].PhoneNum);
printf("\n");
}
break;
case 4:
printf("Goodbye!");
break;
}
}while (Choice != 4);
return 0;
}
这是我运行代码时得到的输出。
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
1
You chose to add a contact.
First name: Test
Last name: 1
Phone number (Numbers only): 1234567
Record added to the phone book
a is 0
n is 2
New contact:
First name: Test
Last name: 1
Phone number: 1234567
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
1
You chose to add a contact.
First name: test
Last name: 2
Phone number (Numbers only): 8901234
Record added to the phone book
a is 1
n is 3
New contact:
First name: test
Last name: 2
Phone number: 8901234
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
3
You chose to show the contacts.
Contact #0
First name: Pq
Last name: q
Phone number: 1095516483
Contact #1
First name: test
Last name: 2
Phone number: 8901234
答案 0 :(得分:3)
您的代码中存在一些问题:
1>变量a,b,con的范围应在主函数中,您可以通过在case块代码中重新声明变量,将变量a,b的范围从主函数范围更改为大小写范围。 >
EXPECT_NE(NullPtr, ptr);
以防万一,请为他们分配新值:
int b = b+1;
int a = (n-1);
struct contact *con = (struct contact*)malloc(n*sizeof(struct contact));
2> con 指针应从
更改为b = b+1;
a = (n-1);
con = (struct contact*)malloc(n*sizeof(struct contact));
通过定义的初始化大小。并且如果大小超出范围,则在添加新项目的情况下,我们需要重新分配 con :
struct contact *con[200];
这是我的解决方案:
int size = 200;
struct contact *con = (struct contact *)malloc(size * sizeof(struct contact));
// in case of the size of out of range
if (n >= size)
{
size = size * 2;
con = (struct contact*)realloc(con, size * sizeof(struct contact));
}
答案 1 :(得分:1)
正如@Some程序员的建议,如果要使其更大并保留数据,则需要重新分配数组。
基本上就是这样
// Records the information given into the structure
struct contact *con = (struct contact*)malloc(n*sizeof(struct contact));
应位于main
函数的开头,如下所示:
// dynamically allocate memory which can hold e.g. 1 struct
struct contact *con = malloc(sizeof(struct contact));
当您调整堆上的内存块大小时,这就是malloc为您执行的操作,您应该像这样调整大小或realloc
:
// grow array
con = realloc(con, n * sizeof(struct concat));
阅读手册页以获取更多信息:realloc man page,或在终端上输入man 3 realloc
或man realloc
。
编辑 正如@dbush所建议的
您声明2个名为con的变量。在struct contact *con[200];
开头和switch语句中,分配内存并将其保存到struct concat *con
中。
所以为什么在选项“ 3”上仍然得到未定义的结果(显示所有联系人)的原因是,实际上是从数组结构contact *con[200];
而不是从{{1指向的内存中读取}}
问候
答案 2 :(得分:1)
您正在con
语句中定义一个名为switch
的新变量。此变量掩码,该变量的名称与函数顶部定义的名称相同。这是您要添加记录的记录。
稍后再打印列表时,它实际上是从内部con
中读取的。但是,由于您要重新输入switch
语句,因此变量的内容未初始化,因为由于切换而跳过了初始化程序,并调用了undefined behavior。您很幸运,您能够打印出最后一个读入的实例,这可能是因为变量仍然碰巧包含了上次迭代中的旧值。
此外,con
的外部版本被声明为指针数组。您可能只想在此处使用一个指针,以便它可以指向动态分配的数组。
这样定义con
:
struct contact *con = NULL;
并更改“ add”大小写以不定义变量。另外,您不需要单独的变量即可读取值。您可以将它们直接读入struct实例:
n++;
con = realloc(n*sizeof(struct contact));
printf("\nYou chose to add a contact.");
printf("\nFirst name: ");
scanf("%s", con[n-1].FirstName);
printf("\nLast name: ");
scanf("%s", con[n-1].LastName);
printf("\nPhone number (Numbers only): ");
scanf("%d", &con[n-1].PhoneNum );
printf("\nRecord added to the phone book");
printf("\nn is %d", n);
printf("\nNew contact:");
printf("\nFirst name: %s", con[n-1].FirstName);
printf("\nLast name: %s", con[n-1].LastName);
printf("\nPhone number: %d", con[n-1].PhoneNum);
printf("\n");
然后,您还需要将a
下移到“显示”部分:
for( int a = 0; a < (n - 1); a++){