2013-03-13 2 views
0

나는 contactList.txt에서 읽는 포인터를 사용하여 함수로 조작되는 이중 연결 목록을 사용하여 연락처 관리자를 코딩합니다. ?이중 연결 목록에 텍스트 파일 읽기

#include<stdio.h> 
#include<conio.h> 
#include<string.h> 
#include<process.h> 
#include<stdlib.h> 
#include<dos.h> 

//functions 
listelement * getFirst(listelement *listpointer,string query[MAX]); 
void getLast(); 
void getEmail(); 
void getCompany(); 
void getNumber(); 
void editCon(); 
void delCon(); 
void addCon(); 
void listAll(); 
void sortCon(); 
void Menu (int *choice); 

#define MAX 20 
//struct to order contactList 
struct contact 
{ 
    string firstName[MAX],lastName[MAX],email[MAX],companyName[MAX]; 
    long phoneNum[MAX]; 
    struct listelement *link 
    struct contact *next; 
    struct contact *prev; 

}list; 



int main() 
{ 
    listelement listmember, *listpointer; 
    string query;int iChoice = 0; 
    listpointer = NULL; 


    Menu (&iChoice); 
    int iChoice; 

    fflush(stdin); 
    scanf_s("%d", &iChoice); 
    // user enters one of 9 values 
    // options are as follows: get first name,last name,list all contacts,search through contacts,add a new contact,edit/delete or sort contacts. 
    switch(iChoice) 
    { 
     case 1:  
     { 
      printf ("Enter contact first name to get details "); 
      scanf ("%d", &query); 
      listpointer = getFirst (listpointer, query); 
      break; 
     } 
     case 2:  
     { 
      getLast(); 
      break; 
     } 
     case 3:  
     { 
      listAll(); 
      break; 
     } 
     case 4:  
     { 
      getEmail(); 
      break; 
     } 
     case 5:  
     { 
      getCompany(); 
      break; 
     } 
     case 6:  
     { 
      getNumber(); 
      break; 
     } 
     case 7:  
     { 
      addCon(); 
      break; 
     } 
     case 8:  
     { 
      editCon(); 
      break; 
     } 
     case 9:  
     { 
      delCon(); 
      break; 
     } 
     case 10:   
     { 
      sortCon(); 
      break; 
     } 
     case 11:  // exit 
     { 
      printf("\n\nProgram exiting!..."); 
      exit(0);//terminates program 
      break; 
     } 
     default: 
     printf ("Invalid menu choice - try again\n"); 
     break; 

    }//end of switch 
    return(iChoice); 


}//end of main 
//menu function to test if invalid input was entered in a menu choice. 
void Menu (int *iChoice) 
{ 
    char local; 

    system("cls"); 
    printf("\n\n\t\\n\n"); 
    printf("\n\n\t\tWelcome to my Contact Manager\n\n"); 
    printf("\n\t\t1. First name"); 
    printf("\n\t\t2. Last name"); 
    printf("\n\t\t3. List all contacts"); 
    printf("\n\t\t4. Search email"); 
    printf("\n\t\t5. Search company name"); 
    printf("\n\t\t6. Search number"); 
    printf("\n\t\t7. Add contact"); 
    printf("\n\t\t8. Edit contact"); 
    printf("\n\t\t9. Delete contact"); 
    printf("\n\t\t10. Sort contacts"); 
    printf("\n\t\t11. Exit"); 
    printf("\n\n\t\tEnter your menu choice: "); 

    do 
    { 
    local = getchar(); 
    if ((isdigit(local) == FALSE) && (local != '\n')) 
     { 
     printf ("\nYou must enter an integer.\n"); 
     printf (""); 
    } 
    } while (isdigit ((unsigned char) local) == FALSE); 

    *iChoice = (int) local - '0'; 
} 

//function to get a contact by entering first name 
listelement * getFirst (listelement *listpointer, string query) 
{ 
    //variables 
    char query[MAX],firstName[MAX]; 
    FILE *fp, *ft; 
    int i,n,ch,l,found; 

    system("cls"); 
    do 
    { 
    found=0; 


    l=strlen(query); 
    fp=fopen("ContactList.txt","r"); 

    system("cls"); 
    printf("\n\n..::Search result for '%s' \n===================================================\n",query); 
    while(fread(&list,sizeof(list),1,fp)==1) 
    { 
    for(i=0;i<=l;i++) 
    firstName[i]=list.firstName[i]; 
    firstName[l]='\0'; 
    if(stricmp(firstName,query)==0) 
    { 
    printf("\n..::First Name\t: %s\n..::Second Name\t: %ld\n..::Email\t: %s\n..::CompanyName\t: %s\n..::Number\t: %s\n",list.firstName,list.lastName,list.email,list.companyName.list.phoneNumber); 
    found++; 
    if (found%4==0) 
    { 
    printf("..::Press any key to continue..."); 
    getch(); 
    } 
    } 
    } 

    if(found==0) 
    printf("\n..::No match found!"); 
    else 
    printf("\n..::%d match(s) found!",found); 
    fclose(fp); 
    printf("\n ..::Try again?\n\n\t[1] Yes\t\t[11] No\n\t"); 
    scanf("%d",&ch); 
    }while(ch==1); 



} 

사람은 내가 코드에서 잘못된거야 곳으로 같은 생각을 가지고 감사

답변

4

귀하의 오류 때문됩니다

1) 당신은 listelement 어디

정의하지 않습니다 2) 어디서나 string을 정의하지 마십시오 (C 유형이 아님)

3) 위의 #define MAX을 위로 이동해야합니다. 전에을 사용하십시오. 다음

4) 당신은 어디 FALSE를 정의하지 않습니다 (그리고 getFirst() 당신이 "문자열"로 query에 전달했습니다에, 당신은 너무 요소를 다시 정의하고 C의 형)

5) 아니다, query을 문자 배열로 새롭게 정의하십시오.

6) 둘 이상의 정의가 있으므로 재 정의 오류가 발생합니다. 그것은 다소 # 5이지만 더 많이 있습니다. 주에서 당신은 iChoice 여기 선언 : string query;int iChoice = 0;이 은 다음

7)

표준 C에 따라 정의되지 않은 동작이 있다는 fflush(stdin)하지 마십시오 int iChoice; 옳은 your Menu() 후 다시 선언