当我调用函数Get_Keywords
时,头指针总是重置为主函数的最后输入值。我想不通。我在另一个测试文件中复制了链表逻辑,它执行得很好。我还在另一个从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;
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;
};
#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;
}
我怎样才能解决这个问题?