1

I'm having trouble implementing a linked list, and returning the values of the list to the screen. Please excuse if this has been asked before - I couldn't find it, and perhaps I don't know how to ask the question correctly!

I am only testing out this code before I build it out for more functionality, but I can't seem to get off the ground. I've commented out some code here, for possible future use. I did not include the other methods below, only Display, particularly because there is nothing in them.

I ran cout statements to see that I am running into problems at statements like: person->lname. This will compile, but will not run. When I try to debug, I get an error withing the string header.

Any help is greatly appreciated!

      #include <iostream>
        #include <string>
        using namespace std;

    struct Birthday
    {
        int month;
        int day;
        int year;
    };

    struct Anniversary
    {
        int month;
        int day;
        int year;
    };

    struct Address
    {
        string street;
        string city;
        string state;
        int zip;
    };

    struct PeopleInfo
    {
        string lname;
        string fname;
        Address fulladdr;
        Birthday bday;
        Anniversary aday;
        //PeopleInfo* nextperson;
    };

    class AddressBook
    {
    public:
        AddressBook();
        AddressBook(string);
        void NewPerson();
        void DeletePerson();
        void SetName();
        void SetAddress();
        void SetDates();
        void Search(string);
        void Sort();
        void Display();
        void BDayCard();
        void ADayCard();
    private:
        PeopleInfo* person;
        //PeopleInfo* currentperson; 
        int length;
    };

    AddressBook::AddressBook()
    {
        person->lname = "last";
        person->fname = "first";
        person->fulladdr.street = "default st.";
        person->fulladdr.city = "anytown";
        person->fulladdr.state = "NJ";
        person->fulladdr.zip = 00000;
        person->bday.month = 0;
        person->bday.day = 0;
        person->bday.year = 0;
        person->aday.month = 0;
        person->aday.day = 0;
        person->aday.year = 0;
        length = 0;
        //person->nextperson->lname = NULL;

        //currentperson->nextperson->lname = NULL;
    }
    void AddressBook::Display()
    {
        cout << person->lname << ", " << person->fname << " " << endl
            << person->fulladdr.street << endl
            << person->fulladdr.city << ", " << person->fulladdr.state << " " << person->fulladdr.zip << endl
            << "Birthday: " << person->bday.month << "/" << person->bday.day << "/" << person->bday.year << endl
            << "Anniversary: " << person->aday.month << "/" << person->aday.day << "/" << person->aday.year << endl << endl;
    }

// Function Prototypes
void Menu(char &entry);
void Action(char &entry, AddressBook AllNames);

// Main
int main()
{
    cout << "test\n";
    char entry;
    cout << "test\n";
    AddressBook AllNames;
    cout << "test\n";
    do
    {
        Menu(entry);
        Action(entry, AllNames);
    } while (!(entry == 'E' || entry == 'e'));
return 0;
}

void Menu(char &entry)
{
    cout << "\nADDRESSBOOK\n\n"
        << "(N) Enter New Name\n"
        << "(D) Delete a Name\n"
        << "(C) Change a Name/Date\n"
        << "(U) Update Anniversary/Birthday\n"
        << "(S) Show Address Book Entries\n"
        << "(B) Make a Birthday Card\n"
        << "(A) Make an Anniversary Card\n"
        << "(E) Exit Program\n\n";
    cin >> entry;
    cout << "\n";
}

void Action(char &entry, AddressBook AllNames)
{
    switch (entry)
    {
    case 'N':
    case 'n': AllNames.NewPerson(); break;
    case 'D':
    case 'd': AllNames.DeletePerson(); break;
    case 'C':
    case 'c': AllNames.SetName(); break;
    case 'U':
    case 'u': AllNames.SetDates(); break;
    case 'S':
    case 's': AllNames.Display(); break;
    case 'B':
    case 'b': AllNames.BDayCard(); break;
    case 'A':
    case 'a': AllNames.ADayCard(); break;
    case 'E':
    case 'e': break;
    default: "\nPlease input a valid entry\n";      
    }
}
2
  • I see no trace of things you wrote in your title. Commented Dec 19, 2015 at 21:25
  • ";" is needed after class AddressBook Commented Dec 19, 2015 at 21:26

1 Answer 1

1

You need to initialize "person".

You have the definition "PeopleInfo* person" inside the "AllNames" class, and this means that person is a pointer to memory of type "PeopleInfo".

The thing is that it doesn't actually point to anything. You are only saying that it could point to such a class.

As "person" is null or garbage (you didn't make it anything), trying to assign something to person->lname will cause the program to crash. If you try to debug the contents of person->lname, that would also crash.

To solve the problem:

Before the first line of the AllNames constructor (right before "person->lname = "last";") put the command:

person = new PeopleInfo();

Sign up to request clarification or add additional context in comments.

2 Comments

!!!Of course! Thank you so much! It's been bothering me since last night! I tried initializing all of the members in the structs and commented out the constructor, but ran into the same problem in Display. Thank you thank you thank you!
It is a standard bug that is encountered frequently. You have joined the ranks of endless programmers who were frustrated about that. Welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.