2013-10-20 2 views
0

Get_Keywords 함수를 호출하면 헤드 포인터가 항상 주 기능의 마지막 입력 값 으로 재설정됩니다. 나는 그것을 이해할 수 없다. I 다른 테스트 파일에서 연결된 목록 로직을 복제했으며 단지 의 성능을 보였습니다. 나는 또한 main에서 이 호출되는 다른 클래스 드라이브 파일에서 비슷한 연결 목록을 시도했다. 이 테스트의 유일한 차이점은 메인에서 클래스로 전달 된 값이 없다는 것입니다.연결할 링크 목록을 가져올 수 없습니다.

숙제를 할당 할 때이 방법으로 프로그램을 추상화해야합니다.

#include "emaillist.h" 
using namespace std; 

email::email() //Default constructor 
{ 
    head = NULL;  
    tail = temp = current = head; 

    kwhead = NULL; 
    kwtail = NULL; 
    kwcurrent = NULL; 
    kwreference = NULL; 
} 

email::~email() //Destructor 
{ 
    if(head) 
    { 
     delete head->emailm.date; 
     delete head->emailm.time; 
     delete head->emailm.from_address; 
     delete head->emailm.to_address; 
     delete head->emailm.subject_title; 
     delete head->emailm.body; 

     delete head; 
     delete kwhead; 
    } 

    head = NULL; 
    tail = NULL; 
    temp = NULL; 
    current = NULL; 

    kwhead = NULL; 
    kwtail = NULL; 
    kwcurrent = NULL; 
    kwreference = NULL; 
} 

int email::Load_from_file(char emailfilename[]) 
{ 
    head = new email_node; 
    head->emailm.date = new char[50]; 
    head->emailm.time = new char[50]; 
    head->emailm.from_address = new char[100]; 
    head->emailm.to_address = new char[100]; 
    head->emailm.subject_title = new char[100]; 
    head->emailm.body = new char [300]; 

    return 0; 
} 

void email::Get_keywords(char *&userkwptr) 
{ 
    kwnode *kwtemp = 0; 
    kwtemp = new kwnode; 
    kwtemp->keywordptr = new char[76]; 
    kwtemp->keywordptr = userkwptr; 
    kwtemp->next = 0; 

    cout << userkwptr << endl; 

    if(!kwhead) 
    { 
     cout << "there was no kwhead." << endl; 
     kwhead = kwtemp; 
     kwcurrent = kwtemp; 
     kwtail = kwtemp; 

     cout << "headpointer value only" << kwhead->keywordptr << endl; 
    } 
    else 
    { 
     cout << "there was a kwhead." << endl; 
     kwcurrent->keywordptr = userkwptr; 
     kwcurrent->next = kwtemp; 
     kwcurrent = kwtemp; 
     kwtail = kwtemp; 
    } 
    cout << "The head pointer value is what? " << kwhead->keywordptr << endl; 
    cout << "the keyword pointer is this " << kwcurrent->keywordptr << endl; 

나는이 문제를 해결할 수있는 방법
#include "emaillist.h" 
    using namespace std; 

    void Welcome_mess(); 
    int Menu(); 
    void Read_all_emails(); 


    void Welcome_mess() 
    { 

     cout << "The purpose of this program is to have the user entere in a list" << endl; 
     cout << "of words to use as search items to a body of emails stored in a " << endl; 
     cout << "separate file. The program will then sort the words the user wants" << endl; 

     cout << "which contain the users keywords" << endl << endl; 
    } 

    int Menu(email &firstsearch) 
    { 
     return 0; 
    } 


    int main() 
    { 
     email(); 
     email firstsearch; 

     char *userkwptr; 
     userkwptr = new char[76]; 

     char *filename; 
     filename = new char[76]; 
     filename = "emails.txt"; 

     Welcome_mess(); 

     int menuchoice = 13; 
     int wordlength = 0; 


     while(menuchoice != 6) 
     { 
      cout << "Menu" << endl << endl; 
      cout << "Please enter one of the corresponding chocies." << endl << endl; 

      cout << "1 = Enter Keywords" << endl; 
      cout << "2 = Display Keywords." << endl; 
      cout << "3 = Search for junk e mails." << endl; 
      cout << "4 = LOAD all of the junk e mails and display them 
       in sequential order by date." << endl; 
      cout << "6 = Exit the program" << endl; 

      cin >> menuchoice; 
      cout << endl; 

      switch(menuchoice) 
      {  
       case 1: 
        cout << "Please enter your first keyword." << endl; 
        cin.ignore(); 
        cin.getline(userkwptr, 75, '\n'); 
        firstsearch.Get_keywords(userkwptr); 
        wordlength = strlen(userkwptr); 

        while(wordlength != 0) 
        { 
         cout << "Please enter your next keyword." << endl; 
         cin.getline(userkwptr, 75, '\n'); 
         firstsearch.Get_keywords(userkwptr); 
         wordlength = strlen(userkwptr); 
        } 

        menuchoice = 0; 
        break; 
       case 2: 
        firstsearch.Display_keywords(); 
        menuchoice = 0; 
        break; 
       case 3: 

        menuchoice = 0; 
        break; 
       case 4: 

        menuchoice = 0; 
        break; 
       case 6: 
        cout << "You have successfully exited the Menu." << endl; 
        menuchoice = 6; 
        return 0; 
        break; 
       default: 
        cout << "There was an error with the menu" << endl; 
        cout << "Please see the tutor." << endl; 

        return menuchoice; 
        break; 
      } 
     } 



     cout << endl << endl; 

     system("PAUSE"); 

     return 0; 
    } 

void email::Display_keywords() 
    { 

     cout << "testing 1, 2, 3, testing " << kwhead->keywordptr << endl; 
     kwcurrent = kwhead; 
     while(kwcurrent->next != NULL) 
     { 
      cout << kwcurrent->keywordptr << endl; 
      kwcurrent = kwcurrent->next; 
     } 

     kwcurrent = kwhead; 
     return; 
    } 

    int email::Find_junk_email() 
    { 
     return 0; 
    } 

#include <iostream> 
    #include <cstring> 
    #include <cctype> 
    #include <fstream> 
    using namespace std; 

    struct email_data 
    { 
     char *date; 
     char *time; 
     char *from_address; 
     char *to_address; 
     char *subject_title; 
     char *body; 
    }; 

    struct email_node 
    { 
     email_data emailm; 
     email_node *next; 
    }; 

    struct kwnode 
    { 
     char *keywordptr; 
     kwnode *next; 
    }; 

    class email 
    { 
    public: 
     email(); 
     ~email(); 
     int Load_from_file(char emailfilename[]); 
     void Get_keywords(char *&userkwptr); 
     void Display_keywords(); 
     int Find_junk_email(); 


    private: 
     email_node *head; 
     email_node *tail; 
     email_node *temp; 
     email_node *current; 

     kwnode *kwhead; 
     kwnode *kwtail; 
     kwnode *kwcurrent; 
     kwnode *kwreference; 
    }; 

?

+1

Welcome to StackOverflow. 나는 당신을 위해 당신의 질문을 형식화하려고 시도했지만 거기에 엄청난 양의 코드가 있습니다. 이 문제를 SSCCE로 해결할 수 있다면 답을 얻거나 (직접 문제를 해결할 가능성이 더 큽니다) 자세한 내용은 http://sscce.org/를 참조하십시오. 행운을 빕니다! – Johnsyweb

답변

0

내가 알 수있는 한 Get_keywords 함수에 대한 첫 번째 항목은 머리가 없어서 if의 실제 블록이 실행되고 생성 한 새 임시 노드가 kwhead에 할당되었지만 보이지 않습니다. 다음에 0 이외의 값이 지정됩니다. 즉, 헤드 노드와 별개의 링크 된 목록이 있음을 의미합니다. 코드를 디버깅하고 생성 된 목록을 살펴 보았습니까?

+0

나는이 코드에서 내가 가진 많은 문제점 중 하나라고 생각한다. 입력 문제는 cin.ignore() 및 cin.clear()를 올바르게 사용하지 않아서 노드에 입력 값이 어떤 선형 함수로 제어되지 않았는지 문제였습니다. 이 poroblem을 볼 시간을내어 주셔서 감사합니다. – user2901222