如何将数据从文本文件填充到对象数组

时间:2019-03-25 19:35:54

标签: c++ populate

问题

我正在构建一个电话簿程序,该程序允许用户执行各种操作,例如添加,查找,编辑和删除联系信息。有了这些,它将不断地发送到文本文件,并在需要时更新文本文件中的数据。

我的问题是,如果我添加一个联系人“ A”,关闭程序,添加另一个联系人“ B”,我的文本文件将覆盖以前的内容,只有“ B”。我希望联系人“ A”和“ B”都在其中。

尝试

在我的appendToFile函数中(需要更改名称,因为它会覆盖而不是追加),我知道每次运行该程序时,它将使用新的电话簿覆盖信息。我之所以选择不附加,是因为当我使用其他功能(例如编辑联系人和删除联系人)时,它应该通过覆盖文件来更新文件。

问题

我如何使用txt中的现有数据,使用该信息填充我的phoneBook [],并保留所有现有数据,同时能够添加新联系人而不删除现有数据?

代码

.h

 #include <string>
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #pragma once


    class Contact
    {
    public:
        Contact();

        std::string firstName;
        std::string lastName;
        std::string name; //lName + fName
        std::string phoneNumber;
        std::string address;
    };


    class AddressBook
    {
    private:

        Contact contact;
        std::ofstream myFile;
        int length;
        static const int maxSize = 10;
        Contact* phoneBook[maxSize]; //array of contact pointers

    public:
        AddressBook();

        void addContact(std::string fName, std::string lName, std::string pNumber, std::string addr); //good
        void deleteContact(std::string nameMatch); //fix
        int findContact(std::string name);
        void editContact();
        void getCurrent();  //good
        void makeEmpty(); //good
        bool isFull(); //good
        bool isEmpty();
        void bubbleSort(Contact* phoneBook[], int n);
        void appendToFile(Contact* phoneBook[]);
        //int validation(Contact* phoneBook[]); //checks if name has numbers or not

        ~AddressBook();
    };


    #include "AddressBook.h"

    AddressBook::AddressBook()
    {
        length = 0;
        phoneBook[maxSize];
    }


    void AddressBook::addContact(std::string fName, std::string lName, std::string pNumber, std::string addr) 
    {
        // need validation

        if (isFull())
        {
            std::cout << "Is full" << std::endl;
            return;
        }

        Contact *contact = new Contact;

        contact->firstName = fName;
        contact->lastName = lName;
        contact->name = lName + ", " + fName;
        contact->phoneNumber = pNumber;
        contact->address = addr;

        std::cout << contact->name + " has been added!" << std::endl;
        phoneBook[length] = contact;
        length++; 
        bubbleSort(phoneBook, length);
        appendToFile(phoneBook); 
    }


    void AddressBook::deleteContact(std::string nameMatch) //need to implement find contact && not found if there is a space at end of name
    {
        if (isEmpty() != true)
        {
            auto position = findContact(nameMatch);
            if (position != -1) //contact not found
            {
                std::cout << phoneBook[position]->name << " deleted" << std::endl;
                phoneBook[position] = phoneBook[length - 1];
                length--;
            }
            bubbleSort(phoneBook, length);
            appendToFile(phoneBook);
        }
            return;
    }

.cpp

 int AddressBook::findContact(std::string nameMatch)
    {
        //implements binary search

        if (isEmpty() != true)
        {

            int first = 0;
            int last = length - 1;
            int middle;
            int position = -1;
            bool found = false;

            while (!found && first <= last)
            {
                middle = (first + last) / 2;


                if (phoneBook[middle]->name == nameMatch)
                {
                    found = true;
                    position = middle;
                }

                else if (phoneBook[middle]->name > nameMatch)
                {
                    last = middle - 1;
                }
                else
                {
                    first = middle + 1;
                }
            }

            if (found == false)
            {
                std::cout << "Person: " << nameMatch << " was not found" << std::endl;
            }

            if (found == true)
            {
                std::cout << phoneBook[position]->name << std::endl;
                std::cout << phoneBook[position]->phoneNumber << std::endl;
                std::cout << phoneBook[position]->address << std::endl;
            }
            return position;
        }
        return 0;
    }


    void AddressBook::editContact() //needs to update file
    {
        if (isEmpty() != true)
        {

            std::string fName, lName, newfName, newlName, newName, newpNum, newAddr, nameMatch;

            //enter old info
            std::cout << "Enter the first name of the contact you want to edit: ";
            std::cin.ignore();
            std::getline(std::cin, fName);

            std::cout << "Enter the last name of the contact: ";
            std::getline(std::cin, lName);

            nameMatch = lName + ", " + fName;
            auto position = findContact(nameMatch);


            if (position != -1)
            {
                //enter new (edited) info
                std::cout << "Enter the new first name: ";
                getline(std::cin, newfName);

                std::cout << "Enter the new last name: ";
                getline(std::cin, newlName);

                newName = newlName + ", " + newfName;
                phoneBook[position]->name = newName;
                phoneBook[position]->firstName = newfName;
                phoneBook[position]->lastName = newlName;

                std::cout << "Enter the new phone number: ";
                getline(std::cin, newpNum);
                phoneBook[position]->phoneNumber = newpNum;

                std::cout << "Enter the new address: ";
                getline(std::cin, newAddr);
                phoneBook[position]->address = newAddr;
            }
        }
        appendToFile(phoneBook);
    }


    void AddressBook::getCurrent()
    {
        if (isEmpty() != true)
        {
            std::cout << "Name: " << phoneBook[length - 1]->name << std::endl;
            std::cout << "Phone Number: " << phoneBook[length - 1]->phoneNumber << std::endl;
            std::cout << "Address: " << phoneBook[length - 1]->address << std::endl;
        }
    }


    void AddressBook::makeEmpty()
    {
        length = 0;
    }


    bool AddressBook::isFull()
    {
        if (length == maxSize)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    bool AddressBook::isEmpty()
    {
        if (length == 0)
        {
            std::cout << "Phonebook is empty" << std::endl;
            return true;
        }
        return false;
    }


    void AddressBook::bubbleSort(Contact * phoneBook[], int n)
    {
        if (!isEmpty())
        {
            Contact* temp;
            int j = 0;

            for (int i = 0; i < length; i++)//for n-1 passes
            {
                for (int j = 0; j < length - 1; j++)
                {
                    if (phoneBook[j]->name > phoneBook[j + 1]->name)
                    {
                        temp = phoneBook[j];
                        phoneBook[j] = phoneBook[j + 1];
                        phoneBook[j + 1] = temp;
                    }
                }
            }
        }
    }


    void AddressBook::appendToFile(Contact* phoneBook[])
    {
        myFile.open("fileName.txt", std::ios::out);
        if (!myFile)
        {
            std::cerr << "Error: file could not be opened." << std::endl;
            return;
        }

        for (int i = 0; i < length; i++)
        {
            myFile << std::endl << phoneBook[i]->name << std::endl;
            myFile << phoneBook[i]->phoneNumber << std::endl;
            myFile << phoneBook[i]->address << std::endl;
            std::cout << std::endl << std::endl;
        }
        myFile.close();
    }
    /*
    int AddressBook::validation(Contact * phoneBook[])
    {
        for (int i = 0; i < phoneBook[length]->name.length; i++)
        {
            if (isalpha(phoneBook[i]->name)==0)
            {

            }
        }
        return false;
    }
    */

    AddressBook::~AddressBook()
    {
        //delete ?

    }


    Contact::Contact()
    {
        std::string fName = "empty";
        std::string lName = "empty"; 
        std::string pNumber = "xxx-xxx-xxxx";
        std::string addr = "empty";
    }

来源

#include "AddressBook.h"
using namespace std;
int main()
{
    AddressBook addrBook;
    string fName, lName, pNum, addr, nameMatch;
    int choice;
    char input;
    do
    {
        cout << endl << "Phone Book" << endl << endl;
        cout << "1. Add Contact" << endl;
        cout << "2. Find Contact" << endl;
        cout << "3. Edit Contact" << endl;
        cout << "4. Delete Contact" << endl;
        cout << "5. Display Current Contact" << endl;
        cout << "0. Quit" << endl;

        do
        {
            cout << endl << "What is your choice?" << endl;
            cin >> choice;
            if (choice < 0 || choice >= 6)
                cout << "That's not an option. Try again." << endl;
        } while (choice < 0 || choice >= 6);

        switch (choice) //need to fix menu
        {
        case 1:
            cout << "Enter your information." << endl;
            cout << "First name: ";
            cin >> fName;
            cout << "Last name: ";
            cin >> lName;
            cout << "Phone Number: ";
            cin.ignore();
            getline(cin, pNum, '\n');
            cout << "Address: ";
            getline(cin, addr, '\n');
            cout << endl;
            addrBook.addContact(fName, lName, pNum, addr);
            break;
        case 2: //finds contact but couts nothing
            cout << "Find Contact" << endl;
            cout << "Enter the name of the contact you want to find." << endl;
            cout << "First name: ";
            cin >> fName;
            cout << "Last name: ";
            cin >> lName;
            nameMatch = lName + ", " + fName;
            cout << endl;
            addrBook.findContact(nameMatch);
            break;
        case 3:
            addrBook.editContact();
            break;
        case 4:
            cout << "Delete Contact" << endl;
            cout << "Enter the first name of the contact you want to delete: ";
            cin.ignore();
            getline(cin, fName);
            cout << "Enter the last name of the contact you want to delete: ";
            getline(cin, lName);
            nameMatch = lName + ", " + fName;
            addrBook.deleteContact(nameMatch);
            break;
        case 5:
            cout << "Display Current Contact" << endl;
            addrBook.getCurrent();
            break;
        case 0:
            cout << "Goodbye" << endl;
            exit(0);
            break;
        }
        cout << "Would you like to continue? (Y/N): ";
        cin >> input;
        system("CLS");
    } while (input == 'Y' || input == 'y'); 
    return 0;
}**

0 个答案:

没有答案