#include <stdio.h>
#include <string.h>
struct Directory {
char Name;
long int Number;
int HouseNumber;
char street;
};
int main(void)
{
struct Directory contact;
struct Directory *answer1, *answer2, *answer3, *answer4;
char answer;
printf("Welcome to Telephone Directory.\n\nPlease enter the name of the contact.\n");
scanf("%s", &contact.Name);
printf("Please enter the number of the contact.\n");
scanf("%ld", &contact.Number);
printf("Please enter the address of the contact.\n");
scanf("%d %s", &contact.HouseNumber, &contact.street);
answer1 = &contact;
answer2 = &contact;
answer3 = &contact;
answer4 = &contact;
printf("Would you like to obtain information on your contact? Enter 'yes' or 'no'.\n");
scanf("%s", &answer);
if (strcmp(&answer, "yes")==0) {
printf("%s\n", &answer1->Name);
printf("%ld\n", answer2->Number);
printf("%d", answer3->HouseNumber);
printf("%s", &answer4->street);
}
if (strcmp(&answer, "no")==0) {
printf("Thank you for using Telephone Directory.\n");
}
}
我正在尝试在 C 中创建一个联系人程序。我希望该程序打印用户的家庭地址。我在结构中有“HouseNumber”变量,在结构中有“street”变量,将“answer3”设置为 int 变量以显示“HouseNumber”,将“answer4”设置为 char 变量以显示“street”。一起,我希望他们将用户的地址打印在一个字符串中,但是当我运行程序并输入“是”以在输入门牌号和街道后显示联系信息时,程序崩溃,并显示 lldb 错误线程错误。似乎一切都是正确的,因为我的编译器说代码没有问题,但它崩溃了。有人可以帮我吗?