程序没有保存输入的条目?(C++)

Program not saving inputted entries? (C++)

本文关键字:C++ 保存 输入 程序      更新时间:2023-10-16

我应该创建一个类AddressBook,其中包含一个名为Person的类。我的程序几乎可以工作,除了当我添加一个人时,它在命令菜单的下一次迭代中不记得了,并且显示全部显示"你的地址簿中有0个人"。我的代码出了什么问题?

#include <iostream>
#include <string>  
using namespace std;
class AddressBook {
public:
    class Person
    {
    public:
        char firstName[15];
        char lastName[15];
        char personID[15];
    };
    Person entries[100];
    unsigned int total;
    AddressBook()
    {
        total = 0;
    }
    void AddPerson()
    {
        cout << "This is entry number " << (total + 1) << " in your address book. " << endl;
        cout << "What shall we put for the first and last name? Limit both to under 15 characters. Example: Bob Smith" << endl;
        cin >> entries[total].firstName >> entries[total].lastName;
        cout << "What is " << entries[total].firstName << " " << entries[total].lastName << "'s ID code?" << endl;
        cin >> entries[total].personID;
        ++total;
        cout << "..." << endl << "Successfully Added." << endl;
    };
    void DisplayPerson(int i)
    {
        cout << "Entry " << i + 1 << ": " << endl;
        cout << "FIRST NAME: " << entries[i].firstName << endl;
        cout << "LAST NAME: " << entries[i].lastName << endl;
        cout << "ID: " << entries[i].personID << endl;
    };
    void DisplayEveryone()
    {
        cout << "You have " << total << " People in your address book." << endl;
        for (int i = 0; i < total; ++i)
            DisplayPerson(i);
    };

    void SearchPerson()
    {
        char lastname[32];
        cout << "Please enter the last name of the person you wish to find." << endl;
        cin >> lastname;
        for (int i = 0; i < total; ++i)
        {
            if (strcmp(lastname, entries[i].lastName) == 0)
            {
                cout << "Person Found. " << endl;
                DisplayPerson(i);
                cout << endl;
            }
        }
    };


};

int main() {
    char command;
    bool Exit = false;
    while (Exit == false)
    {
        AddressBook Address_Book;
        cout << "---------------COMMANDS---------------" << endl;
        cout << "A: Add Person To Address Book" << endl;
        cout << "S: Search for Person in Address Book" << endl;
        cout << "D: Display Everyone In Address Book" << endl << endl;
        cout << "Type the letter of your command: ";
        cin >> command;
        cout << endl;
        switch (command) {
        case 'A':
        case 'a':
            Address_Book.AddPerson();
            break;
        case 'S':
        case 's':
            Address_Book.SearchPerson();
            break;
        case 'D':
        case 'd':
            Address_Book.DisplayEveryone();
            break;
        default:
            cout << "That is not a valid command. Closing Address Book." << endl;
            cout << endl;
        }
    }
}

原因是在while循环的每次迭代中创建一个新的地址簿,并在迭代结束时将其丢弃:

这个

AddressBook Address_Book; 

创建一个新的地址簿,当您到达其作用域的末尾(即循环的末尾)时,该地址簿将被"丢弃"。

事实上,当你想输入新的条目时,你会买一本新的通讯录吗?不。你先买了这本书,然后(可能在一段时间内)添加条目。将上面的线移动到循环之外。

您的问题在于地址簿的声明。

将其更改为以下内容:

AddressBook Address_Book;
while (Exit == false) {
    //Ask for input and respond.
}

在您的版本中,Address_Book是在while循环开始时声明的。这意味着,每次循环的迭代完成并且执行返回到块的开始时,都会创建一个新的本地Address_Book对象,该对象不知道以前的对象数据。