0

我在使用“更改名称”功能时遇到问题,但我似乎找不到任何问题。其他功能,IE 添加名称或删除名称工作正常。可能是列表中的格式或错误。有什么建议么?注意:从 Xcode 复制到 StackOverflow 时,某些格式可能会出现偏差,因此请记住这一点。

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <time.h>
using namespace std;

class Node
{
public:
Node(string newName)
{
    name = newName;
    nextNode = NULL;
}

string name;
string address;
time_t birthday;
time_t anniversary;
Node *nextNode;
};


Node* pHeadNode;


void DrawMenu()
{
cout << "1. Enter a new name into the address book" << endl;
cout << "2. Delete a name from the address book"  << endl;
cout << "3. Change a name or date in the address book" << endl;
cout << "4. Generate birthday cards" << endl;
cout << "5. Generate anniversary cards" << endl;
cout << "6. Exit the card program"  << endl;
cout << "" << endl;
cout << "You choice: ";
}

Node* FindName(string name)
{
if (pHeadNode)
{
    Node *pCurrentNode = pHeadNode;
    while(pCurrentNode && pCurrentNode->name!=name)
    {
        if (pCurrentNode->name==name)
            return pCurrentNode;

        pCurrentNode = pCurrentNode->nextNode;
    }
}
return NULL;
}

enter code heretime_t MakeTime(int year, int month, int day)
{
tm tm1;
tm1.tm_year = year-1900;
tm1.tm_mon = month;
tm1.tm_mday = day;
tm1.tm_hour = 0;
tm1.tm_isdst = 0;
tm1.tm_min = 0;
tm1.tm_sec = 0;
tm1.tm_wday = 0;
tm1.tm_yday = 0;
return mktime(&tm1);
}

void InsertNewNode(Node* pNewNode)
{
if (pNewNode->name < pHeadNode->name)
{
    pNewNode->nextNode = pHeadNode;
    pHeadNode = pNewNode;
} else {
    Node *pCurrentNode = pHeadNode;
    Node *pPrevNode = pCurrentNode;
    while(pCurrentNode && pCurrentNode->name < pNewNode->name)
    {
        pPrevNode = pCurrentNode;
        pCurrentNode = pCurrentNode->nextNode;
    }
    pNewNode->nextNode = pPrevNode->nextNode;
    pPrevNode->nextNode = pNewNode;
}
}

void AddNewEntry()
{
string name;
string address;
time_t birthday;
time_t anniversary;
cout << "Enter name of new Entry: ";
getline(cin, name);
cin.clear();
cin.sync();

getline(cin, name);

if (pHeadNode)
{
    if (FindName(name))
    {
        cout << "" << endl;
        cout << "Name already exists";
        cout << "" << endl;
        return;
    }
}

cout << "Enter address for new Entry: ";
getline(cin, address);

int bday, bmonth, byear;
cout << "Enter (day month year) of birthday: ";
cin >> bday >> bmonth >> byear;
birthday = MakeTime(byear, bmonth, bday);

int aday, amonth, ayear;
cout << "Enter (day month year) of anniversary: ";
cin >> aday >> amonth >> ayear;
anniversary = MakeTime(ayear, amonth, aday);

if (pHeadNode)
{
    Node* pNewNode = new Node(name);
    pNewNode->address = address;
    pNewNode->birthday = birthday;
    pNewNode->anniversary = anniversary;

    InsertNewNode(pNewNode);
} else {
    pHeadNode = new Node(name);
    pHeadNode->address = address;

    pHeadNode->birthday = birthday;
    pHeadNode->anniversary = anniversary;
}

cout << "" << endl;
cout << "New Entry is added" << endl;
cout << "" << endl;
}

void DeleteEntry()
{
string name;

if (pHeadNode)
{
    cin.clear();
    cin.sync();

    cout << "Enter name of Entry you want to delete: ";
    getline(cin, name);

    Node *pCurrentNode = pHeadNode;
    Node *pPrevNode = pCurrentNode;
    while(pCurrentNode && pCurrentNode->name!=name)
    {
        if (pCurrentNode->name==name)
            break;

        pCurrentNode = pCurrentNode->nextNode;
    }

    if (pCurrentNode)
    {
        if (pCurrentNode==pHeadNode)
        {
            pHeadNode = pCurrentNode->nextNode;
        } else {
            pPrevNode->nextNode = pCurrentNode->nextNode;
        }
        delete pCurrentNode;
    } else {
        cout << "" << endl;
        cout << "Entry is not found" << endl;
        cout << "" << endl;
    }
} else {
    cout << "" << endl;
    cout << "Address book is empty already" << endl;
    cout << "" << endl;
    return;
}
}

void ChangeEntry()
{
string name;
string address;
time_t birthday;
time_t anniversary;

if (pHeadNode)
{
    cin.clear();
    cin.sync();

    cout << "Enter name of Entry you want to edit: ";
    getline(cin, name);

    Node *pCurrentNode = pHeadNode;
    Node *pPrevNode = pCurrentNode;
    while(pCurrentNode && pCurrentNode->name!=name)
    {
        if (pCurrentNode->name==name)
            break;

        pCurrentNode = pCurrentNode->nextNode;
    }

    if (pCurrentNode)
    {
        cin.clear();
        cin.sync();
        cout << "Enter name of new Entry: ";
        getline(cin, name);

        if (pHeadNode)
        {
            if (FindName(name))
            {
                cout << "" << endl;
                cout << "Name already exists";
                cout << "" << endl;
                return;
            }
        }

        if (pCurrentNode==pHeadNode)
        {
            pHeadNode = pCurrentNode->nextNode;
        } else {
            pPrevNode->nextNode = pCurrentNode->nextNode;
        }
        delete pCurrentNode;

        cout << "Enter address for new Entry: ";
        getline(cin, address);

        int bday, bmonth, byear;
        cout << "Enter (day month year) of birthday: ";
        cin >> bday >> bmonth >> byear;
        birthday = MakeTime(byear, bmonth, bday);

        int aday, amonth, ayear;
        cout << "Enter (day month year) of anniversary: ";
        cin >> aday >> amonth >> ayear;
        anniversary = MakeTime(ayear, amonth, aday);

        Node* pNewNode = new Node(name);
        pNewNode->address = address;
        pNewNode->birthday = birthday;
        pNewNode->anniversary = anniversary;

        InsertNewNode(pNewNode);

        cout << "" << endl;
        cout << "Entry is changed" << endl;
        cout << "" << endl;
    } else {
        cout << "" << endl;
        cout << "Entry is not found" << endl;
        cout << "" << endl;
    }
} else {
    cout << "" << endl;
    cout << "Address book is empty " << endl;
    cout << "" << endl;
    return;
}
}

void GenerateBirthdayCards()
{
Node *pCurrentNode = pHeadNode;


time_t t = time(NULL);
tm *tm1 = localtime(&t);
while(pCurrentNode)
{
    tm *ct = localtime(&pCurrentNode->birthday);

    if (ct->tm_mon==tm1->tm_mon && ct->tm_mday==tm1->tm_mday)
    {
        cout << "" << endl;
        cout << "Dear " << pCurrentNode->name << "," << endl;
        cout << "" << endl;
        cout << "Hope your birthday is really wonderful and this coming year is the best yet!" << endl;
        cout << "" << endl;
        cout << "Love," << endl;
        cout << "Joanne" << endl;
        cout << "" << endl;
    }

    pCurrentNode = pCurrentNode->nextNode;
}
}

void GenerateAnniversaryCards()
{
Node *pCurrentNode = pHeadNode;

time_t t = time(NULL);
tm *tm1 = localtime(&t);
while(pCurrentNode)
{
    tm *ct = localtime(&pCurrentNode->anniversary);

    if (ct->tm_mon==tm1->tm_mon && ct->tm_mday==tm1->tm_mday)
    {
        cout << "" << endl;
        cout << "Dear " << pCurrentNode->name << "," << endl;
        cout << "" << endl;
        cout << "Hope your anniversary is really wonderful and this coming year is the best yet!" << endl;
        cout << "" << endl;
        cout << "Love," << endl;
        cout << "Joanne" << endl;
        cout << "" << endl;
    }

    pCurrentNode = pCurrentNode->nextNode;
}
}

void SaveListToFile()
{
ofstream outfile;

outfile.open("list.txt");

Node* pCurrentNode = pHeadNode;
while(pCurrentNode)
{
    Node* pNode = pCurrentNode;
    pCurrentNode = pCurrentNode->nextNode;
    outfile << pNode->name << endl;
    outfile << pNode->address << endl;
    outfile << pNode->birthday << endl;
    outfile << pNode->anniversary << endl;
}

outfile.close();
}

void LoadListFromFile()
{
ifstream infile;

infile.open("list.txt");

string name;

while (getline(infile, name))
{
    string address;
    getline(infile, address);
    time_t birthday;
    time_t anniversary;

    string strBirthday;
    getline(infile, strBirthday);
    istringstream iss1(strBirthday);
    iss1 >> birthday;

    string strAnniversary;
    getline(infile, strAnniversary);
    istringstream iss2(strAnniversary);
    iss2 >> anniversary;

    if (!pHeadNode)
    {
        pHeadNode = new Node(name);
        pHeadNode->address = address;
        pHeadNode->birthday = birthday;
        pHeadNode->anniversary = anniversary;
    } else {
        Node *pCurrentNode = pHeadNode;
        Node *pPrevNode = pCurrentNode;
        while(pCurrentNode)
        {
            pPrevNode = pCurrentNode;
            pCurrentNode = pCurrentNode->nextNode;
        }
        pPrevNode->nextNode = new Node(name);
        pPrevNode->nextNode->address = address;
        pPrevNode->nextNode->birthday = birthday;
        pPrevNode->nextNode->anniversary = anniversary;
    }
    //std::istringstream iss(line);
    //int a, b;
    //if (!(iss >> a >> b)) { break; } // error
    // process pair (a,b)
}


infile.close();
}

int main()
{
int choice;
bool stop = false;

LoadListFromFile();
do
{
    do
    {
        DrawMenu();
        cin >> choice;
        if (choice<1 || choice>6)
            cout << "Incorrect choice. Try again" << endl << endl;
    }while(choice<1 || choice>6);

    switch(choice)
    {
        case 1:
            AddNewEntry();
            break;
        case 2:
            DeleteEntry();
            break;
        case 3:
            ChangeEntry();
            break;
        case 4:
            GenerateBirthdayCards();
            break;
        case 5:
            GenerateAnniversaryCards();
            break;
        case 6:
            stop = true;
            break;
        default:
            //it's impossible,
            break;
    }

}while(!stop);
SaveListToFile();

Node* pCurrentNode = pHeadNode;
while(pCurrentNode)
{
    Node* pNode = pCurrentNode;     
    pCurrentNode = pCurrentNode->nextNode;
    delete pNode;
}

return 0;
}
4

1 回答 1

0

当您首先修改条目时,您删除现有条目,然后插入新节点,而不是实际修改旧条目本身。一旦你这样做了,你调用 InsertNewNode 但你不检查列表是否为空

因此,

void InsertNewNode(Node* pNewNode)
{
    if (pNewNode->name < pHeadNode->name) // <-- If pHeadNode is NULL you crash
    {
        ...
    }
    ...
}

将该行更改为:

    if (!pHeadNode || (pNewNode->name < pHeadNode->name))
于 2013-09-12T18:03:38.300 回答