簡體   English   中英

我的C通訊錄程序有一個小問題

[英]I'm having a minor issue with my address book program in C

我的通訊簿有一個小問題,如果我嘗試加載聯系人數據而沒有輸入並保存至少一個聯系人信息,則程序將返回“ 0”並凍結。 另外,我覺得我也需要在該程序中添加一個remove contact函數,如果有人可以幫助我,那么我將非常感謝= 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的原因。 feof使用NULL文件指針的行為是不確定的,因此可能是掛起的地方。 保留所有權利,該程序應崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM