gpt4 book ai didi

c - 我的 C 通讯录程序遇到一个小问题

转载 作者:行者123 更新时间:2023-11-30 17:41:51 24 4
gpt4 key购买 nike

我的地址簿有一个小问题,如果我在没有输入和保存至少一个联系信息的情​​况下尝试加载联系人数据,程序将返回“0”并卡住。另外,我觉得我还需要在这个程序中添加删除联系人功能,如果有人能帮助我,我将非常感激=D。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define NAME 40
#define LNAME 40
#define ADDRESS 100
#define PCODE 6
#define PNUM 10
#define MAX 10

void Insert();
void Display();
void Search();
void Save();
void Load();
void Exit();

char temp [10];

typedef struct contact //defines the structure for our address book

{
char name [NAME ];
char Lname [LNAME ];
char address[ADDRESS];
char Pcode [PCODE];
char Pnum [PNUM];

}
contact;

int counter = 0;
int placeHolder = 0;
int loadContact;

int fileCount;

int save = 0;

FILE *PFileOut;

contact arrayContact [MAX];


int main()
{
int option;
char filechar;

do
{
printf("\n***Personal Contact Book V1.0***\n\n");
printf("1.Add new contact\n");
printf("2.Display current contacts\n");
printf("3.Search for a contact\n");
printf("4.Save contacts to file\n");
printf("5.Load contacts to file\n");
printf("6.Exit\n\n");
printf("> ");

scanf("%d", &option);

switch (option)
{
case 1:
Insert();
break;

case 2:
Display();
break;

case 3:
Search();
break;

case 4:
Save();
break;

case 5:
Load();
break;

case 6:
Exit();
break;

default:
printf("That is not a valid input, please choose between (1-6)");

}
}
while(option !=6 );
}

void Insert()
{
char option;
if(placeHolder>=10){
printf("Your contact list is full!");
return;
}
do{
printf("Contact Number: %d\n", counter+1);
printf("First name: ");
scanf(" %[^\n]s", arrayContact[counter].name);
printf("Last name: ");
scanf(" %[^\n]s", arrayContact[counter].Lname);
printf("Address: ");
scanf(" %[^\n]s", arrayContact[counter].address);
printf("Postal Code: ");
scanf(" %[^\n]s", arrayContact[counter].Pcode);
printf("Phone: ");
scanf(" %[^\n]s", arrayContact[counter].Pnum);

placeHolder++;
counter++;

printf("Press y/Y if you wish to add another contact, any key to return to main menu\n");
scanf(" %c",&option);
printf("\n");

}while( (option =='y'|| option == 'Y')&& placeHolder<MAX);






}

void Display()
{
counter = 0;
while(counter<placeHolder){
printf("Contact Number: %d\nFirst Name: %s\nLast Name: %s\nAddress: %s\nPostal Code: %s\nPhone: %s\n\n",counter+1,arrayContact[counter].name,
arrayContact[counter].Lname,arrayContact[counter].address,arrayContact[counter].Pcode,arrayContact[counter].Pnum);
counter++;
}
counter = placeHolder;
}

void Search()
{
char search [40];
char compare [40];
int counterLetter;
int counterContact;
int verify = 0;

printf("What is the contact's Last name? ");
scanf(" %s", search);

for( counterLetter = 0; search[ counterLetter ] != '\0'; counterLetter++ ) {
search[ counterLetter ] = tolower( search[ counterLetter ] );
}

for( counterContact = 0; counterContact<placeHolder ; counterContact++ ){
strcpy(compare,arrayContact[counterContact].Lname);

for( counterLetter = 0; compare[ counterLetter ] != '\0'; counterLetter++ ) {
compare[ counterLetter ] = tolower( compare[ counterLetter ] );
}

if( strcmp( search, compare ) == 0 ){
printf("Contact Number: %d\nFirst Name: %s\nLast Name: %s\nAddress: %s\nPostal Code: %s\nPhone: %s\n\n",counterContact+1,
arrayContact[counterContact].name,arrayContact[counterContact].Lname,arrayContact[counterContact].address,arrayContact[counterContact].Pcode,
arrayContact[counterContact].Pnum);
verify = 1;
}
}

if(verify == 0){
printf("No results found");
}

}

void Save()
{
PFileOut = fopen("Contact Book.dat","w");
fprintf(PFileOut,"%d\n",placeHolder);
for(counter = 0;counter<placeHolder;counter++){
fprintf(PFileOut,"\n%s\n%s\n%s\n%s\n%s",arrayContact[counter].name,arrayContact[counter].Lname,arrayContact[counter].address,arrayContact[counter].Pcode,
arrayContact[counter].Pnum);
}
fclose(PFileOut);
save = 1;

printf("\nSuccessfully Saved!!\n");
}

void Load()
{
char temp[40];
PFileOut = fopen("Contact Book.dat","r");
fscanf(PFileOut, "%d", &fileCount);
printf("%d\n", fileCount);
while(!feof(PFileOut)){
fscanf(PFileOut,"%s",&arrayContact[placeHolder].name);
fscanf(PFileOut,"%s",&arrayContact[placeHolder].Lname);
fscanf(PFileOut," %[^\n]s",&arrayContact[placeHolder].address);
fscanf(PFileOut,"%s",&arrayContact[placeHolder].Pcode);
fscanf(PFileOut, " %[^\n]s",&arrayContact[placeHolder].Pnum);
placeHolder++;
}
fclose(PFileOut);
}

void Exit()
{ char option6;
while(save!=1){
printf("It seems you have not saved your progress. Would you like to save? (y/n)");
scanf(" %c", &option6);
if(option6 == 'y'|| option6 == 'Y')
{
Save();
}
else
{
puts ("\nThank you for using Contact Book");

exit(0);
}
}
puts ("\nThank you for using Contact Book");
exit(0);

}

最佳答案

您的程序存在很多问题。

如果 .dat 文件不存在,则 fopen 将失败,文件指针为 NULL。随后的 fscanf 也将不起作用,这就是为什么记录计数为 0。带有 NULL 文件指针的 feof 的行为是未定义的,所以这可能就是你所挂起的地方。按理说,这个程序应该崩溃。

关于c - 我的 C 通讯录程序遇到一个小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20977650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com