Saving Array Data into Text File

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.

Thanks and here's what I have thus far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <string.h> //Required to use string compare                         
#include <fstream> //Eventually used to store array into file 

using namespace std;

class AddBook{

public:
	AddBook()
      {
         count=0;
      }

   void AddEntry();
   void DispAll(); //Displays all entries
   void DispEntry(int i); //Displays one entry                              
   void SearchLast();

   int Menu();

   struct EntryStruct
   {
      char FirstName[15];
      char LastName[15];
      char Birthday[15];
      char PhoneNum[15];
      char Email[15];
   };

   EntryStruct entries[100];
   int count;
};


void AddBook::AddEntry()
{

   cout << "Enter First Name: ";
   cin >> entries[count].FirstName;

   cout << "Enter Last Name: ";
   cin >> entries[count].LastName;

   cout << "Enter Date of Birth: ";
   cin >> entries[count].Birthday;

	cout << "Enter Phone Number: ";
   cin >> entries[count].PhoneNum;

   cout << "Enter Email: ";
   cin >> entries[count].Email;

   ++count;                                                
}


void AddBook::DispEntry(int i)
{                
   cout << "First name : " << entries[i].FirstName << endl;
   cout << "Last name : " << entries[i].LastName << endl;
   cout << "Date of birth : " << entries[i].Birthday << endl;
   cout << "Phone number : " << entries[i].PhoneNum << endl;
   cout << "Email: " << entries[i].Email << endl;
   cout << endl;
}

void AddBook::DispAll()
{
   cout << "Number of entries: " << count << endl;

   for(int i = 0;i < count;++i)
      DispEntry(i);     
}

void AddBook::SearchLast()
{
   char lastname[32];
   cout << "Enter last name: ";
   cin >> lastname;

   for(int i = 0;i < count;++i)
   {
      if(strcmp(lastname, entries[i].LastName) == 0)
      {
         cout << "Displaying all relevant search items ";
         DispEntry(i);
         cout << endl;
      }
   }
}
                                                                    
AddBook AddressBook;

int Menu()
{
   int num;
   bool BoolExit = false;
                                           
   while(BoolExit == false)
   {                     

      cout << endl;
      cout << "############################" << endl;
      cout << "#    Address Book Menu     #" << endl;
      cout << "############################" << endl;
      cout << "# (1) Add A New Contact    #" << endl;
      cout << "# (2) Search By Last Name  #" << endl;
      cout << "# (3) Show Complete List   #" << endl;
      cout << "# (4) Exit And Save        #" << endl;
      cout << "############################" << endl;
      cout << endl;

		cout << "Please enter your selection (1-4) and press enter: ";      
      cin >> num;
      cout << endl;

      if (num == 1)
         AddressBook.AddEntry();
      else if (num == 2)
         AddressBook.SearchLast();
      else if (num == 3)
         AddressBook.DispAll();
      else if (num == 4)
         BoolExit = true;
      else
         cout << "Please enter a number (1-4) and press enter: " << endl;

      cout << endl;                                                            
   }

   return 0;
}

int main (){
   Menu();

   return 0;
}
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..
Last edited on
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();
      else if (num == 2)
         AddressBook.SearchLast();
      else if (num == 3)
         AddressBook.DispAll();
      else if (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 else if (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 forvoid 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 forvoid 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();
- You forgot to change the function declarations in your class at Lines 16 and 17.

- Remember that your DispAll() function is a member function, so it cannot exist without being attached to an instance of your AddressBook class.

- You'll also need to create an instance of an std::ofstream object and pass it to your DispAll() member function when the user chooses option '4'.

- Yes, adding brackets to your if else block will help with that issue.
Last edited on
I'm assuming the following:

1
2
void DispAll(std::ostream &Out = std::cout); //Displays all entries
void DispEntry(int i, std::ostream &Out = std::cout); //Displays one entry     


The rest I'm still trying to work out. This has proven more difficult that I originally anticipated.

I'm coming closer to a solution. I'll give you a heads up once I figure it out.

Thanks again!
Topic archived. No new replies allowed.