Here's my problem. I have a program that saves all information temporarily into memory (into the array), however, I need a permanent save solution.
I have the full program working correctly, formatted perfectly, but it's missing the file array to file output.
Essentially, whenever I'm presented with the menu I'll be able to add entries, search by last name, show the entire list, and exit and save. Whenever I select option 4 the program should save to a file "address_book.txt". Whenever I reload the program, it should load from "address_book.txt" and populate the array with the preexisting data.
My question is focused on how and where I should implement the file output. I apologize if it seems that I'm not solving my own question, I'm just completely stuck.
You could get kind of fancy and modify your DispAll and DispEntry functions to except an std::ostream reference with a default value set to std::cout. Then you would just need to modify the instances of cout in those functions to be the name of the variable you declared for it. Something like this:
1 2 3 4 5 6 7 8 9
void AddBook::DispEntry(int i, std::ostream &Out = std::cout)
{
Out << "First name : " << entries[i].FirstName << endl;
Out << "Last name : " << entries[i].LastName << endl;
Out << "Date of birth : " << entries[i].Birthday << endl;
Out << "Phone number : " << entries[i].PhoneNum << endl;
Out << "Email: " << entries[i].Email << endl;
Out << endl;
}
And this:
1 2 3 4 5 6 7
void AddBook::DispAll(std::ostream &Out = std::cout)
{
Out << "Number of entries: " << count << endl;
for(int i = 0;i < count;++i)
DispEntry(i, Out);
}
This would reduce the amount of code you need to modify. But as always save a copy of your source in it's current working form in a different directory before making any major changes.
EDIT: You need to call DispAll() if the user chooses option 4. Also you should decide if "Menu()" is a member function or not..
Okay, I made those modifications, tried compiling but the result was erroneous.
For the option 4 I did this:
1 2 3 4 5 6 7 8 9 10
if (num == 1)
AddressBook.AddEntry();
elseif (num == 2)
AddressBook.SearchLast();
elseif (num == 3)
AddressBook.DispAll();
elseif (num == 4)
DispAll();
else
cout << "Please enter a number (1-4) and press enter: " << endl;
I'll figure another way to exit the program after it saves, possibly just doing elseif (num == 4){ DispAll(); }
I'm confused as to what's missing or what approach I should take next.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
addbook_dec04b.cpp:59:6: error: prototype for ‘void AddBook::DispEntry(int, std::ostream&)’ does not match any in class ‘AddBook’
void AddBook::DispEntry(int i, std::ostream &Out = std::cout)
^
addbook_dec04b.cpp:18:9: error: candidate is: void AddBook::DispEntry(int)
void DispEntry(int i); //Displays one entry
^
addbook_dec04b.cpp:69:6: error: prototype for ‘void AddBook::DispAll(std::ostream&)’ does not match any in class ‘AddBook’
void AddBook::DispAll(std::ostream &Out = std::cout)
^
addbook_dec04b.cpp:17:9: error: candidate is: void AddBook::DispAll()
void DispAll(); //Displays all entries
^
addbook_dec04b.cpp: In function ‘int Menu()’:
addbook_dec04b.cpp:126:18: error: ‘DispAll’ was not declared in this scope
DispAll();