2014-01-07 3 views
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); 

} 
+0

너무 많은 코드입니다. 디버거를 사용하여 좁히기 –

+0

먼저 기본 C 책/튜토리얼을 읽어야합니다. – haccks

+0

@ed, 나는 미안하다. 이것이 나의 첫 번째 질문이다. 어떻게 디버거를 사용하면 코드를 좁힐 수 있을까? 또한 대부분의 코드를 거기에 썼습니다. 그 문제가 왜 나타나는지 알 수 없습니다. –

답변

0

프로그램에 많은 문제가 있습니다.

.dat 파일이 없으면 파일 포인터를 NULL로 유지하면서 fopen이 실패합니다. 후속 fscanf도 작동하지 않습니다. 따라서 레코드 수가 0이됩니다. NULL 파일 포인터가있는 feof의 동작은 정의되지 않았으므로 아마도 여러분이 매달려있는 곳일 것입니다. 모든 권한으로이 프로그램이 중단되어야합니다.

관련 문제