0
我的地址簿中存在一個小問題,如果我嘗試加載聯繫人數據時未輸入並至少保存一個聯繫人信息,程序返回一個「0」並凍結。另外,我覺得我需要在這個程序中添加一個刪除聯繫人功能,如果有人願意幫我解決這個問題,那麼我會非常感激= D。我的地址簿程序存在一個小問題C
#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);
}
這是太多的代碼。使用調試器縮小範圍 –
您應該首先閱讀基本的C書/教程。 – haccks
@ed,對不起,我是新手,這是我的第一個問題,如何使用調試器縮小代碼範圍?我也寫了大部分的代碼,我只是不知道爲什麼會彈出這個問題 –