How to create database to a notepad file?

We have this pre-lesson topic about notepad database like thing.
Since we have no idea in doing it yet, what can I do to integrate it here in this program? As of now, we can't use the delete, search/sort, and edit feature of this program we made.

Here is the expected output:
http://i.imgur.com/pCDUdqx.png
http://i.imgur.com/U7DikJ2.png


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
140
141
142
143
144
  #include <iostream>
#include <string>                      

using namespace std;

class ABook{

public:
    ABook()
      {
         count=0;
      }

   void AddEntry();
   void DispAll();
   void DispEntry(int i);
   void DeleteEntry();                            
   void SearchLast();
   void SearchFirst();
   void SearchEmail();
   void SearchAddress();
   void EditContact();

   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 ABook::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 ABook::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;
}

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

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

void ABook::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 << "Found ";
         DispEntry(i);
         cout << endl;
      }
   }
}

ABook AddressBook;

int Menu()
{
   int num;
   bool BoolQuit = false;

   while(BoolQuit == false)
   {                     

      cout << "Address Book Menu" << endl;
      cout << "(1) Add Contact" << endl;
      cout << "(2) Edit Contact" << endl;
      cout << "(3) Delete Contact" << endl;
      cout << "(4) View Contacts" << endl;
      cout << "(5) Search Address Book" << endl;
      cout << "(6) Exit" << endl;
      cout << endl;

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

      if (num == 1)
         AddressBook.AddEntry();
      else if (num == 2)
         AddressBook.EditContact();
      else if (num == 3)
         AddressBook.DeleteEntry();
		else if (num == 4)
         AddressBook.DispAll();
      else if (num == 5)
         AddressBook.DispEntry();
      else if (num == 6)
         BoolQuit = true   
      else
         cout << "Please enter a number (1-4) and press enter: " << endl;

      cout << endl;                                                            
   }

   return 0;
}

int main (){
   Menu();

   return 0;
}


Also, there are errors I cannot solve because I'm new to this topic:
Here are some error lines:

http://i.imgur.com/7p2qpLq.png
Last edited on
Firstly, include these headers :
1
2
#include <cstdlib>
#include <cstring> 
Hello DoubleOSeven,

I would suggest two more functions that are useful. One that writes to a file and one that reads from a file.

Andy
Okay. I can write to a notepad file
(Ignore the comments, I'll just state the lines that needs help because the comments are in our native language)

In Line 78: Our teacher wants to display the First Name, Last name, Birthday, Address, and Email address inputted by the user. This works if I previously inputted entries but when the program is freshly started, it will say 0 entries

Also the one in Line 93, if the program is freshly started, it won't display results. Line 102: I cannot delete the lines in the text file.
I cannot also use the search by: First Name, last name, Address and email address function .


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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>                 
#include <fstream>
#include <algorithm>      

using namespace std;

class ABook{						//kailangan daw gumamit ng class. Search nyo na din kung para saan ang class tapos pag-usapan nalang natin

public:								//laging magkasama ang class at public (pati private, pero di tayo gagamit nun dito)
    ABook()
      {
         count=0;
      }

   void AddEntry();
   void DispAll();
   void DispEntry(int i);
   void DeleteEntry();                           
   void SearchLast();				//Di pa napapalabas yung NOT FOUND!
   void SearchFirst();            
   void SearchEmailAddress();     
   void SearchPhoneNum();
   // void EditContact();           //  Wala pa tayo neto.

   int Menu();

   struct EntryStruct				// Pwede ata gawin natin tong input file, prang may mastotorage sa "txt. file" gagamit ng notepad
   {
      char FirstName[15];
      char LastName[15];
      char Birthday[15];
      char PhoneNum[15];
      char Email[15];
   };

   EntryStruct entries[100];
   int count;
};


void ABook::AddEntry()
{

   cout << "Enter First Name: ";
   ofstream a_file ( "contacts.txt", ios::app );			//Babasahin niya yung contacts.txt na file
   cin >> entries[count].FirstName;
   a_file << entries[count].FirstName << "\n";				// Ilalagay nya yung input sa .txt file
cin.clear();
cin.sync();
   cout << "Enter Last Name: ";
   cin >> entries[count].LastName;
   a_file << entries[count].LastName << "\n";				//ganun din dito
cin.clear();
cin.sync();
   cout << "Enter Date of Birth: ";
   cin >> entries[count].Birthday;
   a_file << entries[count].Birthday << "\n";
cin.clear();
cin.sync();
    cout << "Enter Phone Number: ";
   cin >> entries[count].PhoneNum;
   a_file << entries[count].PhoneNum << "\n";
cin.clear();
cin.sync();
   cout << "Enter Email: ";
   cin >> entries[count].Email;
   a_file << entries[count].Email << "\n";
   a_file.close();

   ++count;                                                
}



void ABook::DispEntry(int i)
{   
ifstream a_file ( "contacts.txt" );
	a_file>> entries[i].FirstName;										// di ko pa sure kung gumagana
	a_file>> entries[i].LastName; 
	a_file>> entries[i].Birthday; 
	a_file>> entries[i].PhoneNum; 
	a_file>> entries[i].Email;              
   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;
}

void ABook::DispAll()
{
   cout << "Number of Entries : " << count << endl;

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

void ABook::DeleteEntry()     // KULANG PA TONG PART NA TO! (Christer: Di ko mabura yung laman ng .txt file, tsaka yung code mo Ry, di naman nag-dedelete ng entry)
{
 	char FirstName [32];
  cout << "Enter the first name of the contact you want to be deleted: ";
   cin >> FirstName;
   cout << endl;

   for(int i = 0;i < count;++i)
   {
      if(strcmp(FirstName, entries[i].FirstName) == 0)
      {
      	
         cout << "Deleted";
		 cout << endl;
		 }
  
                                                 
}
}
								 // SIMULA DITO, KUlANG LANG KUNG MSASABING NOT FOUND NA ISAHAN.
								 
void ABook::SearchLast ()
{
   char LastName[32];
   cout << "Enter Last Name : ";
   cin >> LastName;
   cout << endl;

   for(int i = 0;i < count;++i)
   {
      if(strcmp(LastName, entries[i].LastName) == 0)    	// NAGLAGAY AKO DITO NG ELSE IF, tapos baliktad n "!=0" kaso kapag marami ng na-add contact.
      {														//MMay lalabas na found, kaso may lumalabas din na not found"
      														// AKo nalang bahala dito. - RY
         cout << "Found " << endl ;
         DispEntry(i);
         cout << endl;
      }

   }
}

void ABook::SearchFirst()
{
   char FirstName[32];
   cout << "Enter First Name : ";
   cin >> FirstName;
   cout << endl;
   
   for(int i = 0;i < count;++i)
   {
      if(strcmp(FirstName, entries[i].FirstName) == 0)
      {
         cout << "Found " << endl ;
         DispEntry(i);
         cout << endl;
      }
         else if(strcmp(FirstName, entries[i].FirstName) != 0)
       {
         cout << "Not Found" << endl ;
         cout << endl;
      }
   }
}

void ABook::SearchEmailAddress()
{
   char Email[32];
   cout << "Enter Email : ";
   cin >> Email;
   cout << endl;

   for(int i = 0;i < count;++i)
   {
      if(strcmp(Email, entries[i].Email) == 0)
      {
         cout << "Found " << endl ;
         DispEntry(i);
         cout << endl;
      }
         else if(strcmp(Email, entries[i].Email) != 0)
       {
         cout << "Not Found" << endl ;
         cout << endl;
      }
   }
}

void ABook::SearchPhoneNum()
{
   char PhoneNum[32];
   cout << "Enter Phone Number : ";
   cin >> PhoneNum;
   cout << endl;

   for(int i = 0;i < count;++i)
   {
      if(strcmp(PhoneNum, entries[i].PhoneNum) == 0)
      {
         cout << "Found " << endl ;
         DispEntry(i);
         cout << endl;
      }
         else if(strcmp(PhoneNum, entries[i].PhoneNum) != 0)
       {
         cout << "Not Found" << endl ;
         cout << endl;
      }
   }
}


ABook AddressBook;

int Menu()
{
   int num, choice;
   bool BoolQuit = false;

   while(BoolQuit == false)
   {                     

      cout << "Address Book Menu" << endl;
      cout << "(1) Add Contact" << endl;
      cout << "(2) Edit Contact" << endl;
      cout << "(3) Delete Contact" << endl;
      cout << "(4) View Contacts" << endl;
      cout << "(5) Search Address Book" << endl;
      cout << "(6) Exit" << endl;
      cout << endl;

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

      if (num == 1)
         AddressBook.AddEntry();
      /*else if (num == 2)									//di pa pwedeng gamitin kasi kulang pa
         AddressBook.EditContact();*/
      else if (num == 3)
         AddressBook.DeleteEntry();
		else if (num == 4)
         AddressBook.DispAll();
      else if (num == 5)
      {
	  	cout << "Search the Address Book : " << endl;
      	cout << "(1) By Last Name" << endl;
      	cout << "(2) By First Name" << endl;
      	cout << "(3) By Email Address" << endl;
      	cout << "(4) By Phone Number" << endl;
      	cout << endl;
      		cout << "Please enter how do you want to search: ";
      		cin >> choice;
      		cout << endl;
      		
      	switch (choice)
      	 {
      	 	case 1: AddressBook.SearchLast();
      	 	break;
      	 	case 2: AddressBook.SearchFirst();
      	 	break;
      	 	case 3: AddressBook.SearchEmailAddress();
      	 	break;
      	 	case 4: AddressBook.SearchPhoneNum();
      	 	break;
		   }
		}
      else if (num == 6)
         BoolQuit = true  ;
      else
         cout << "Please enter a number (1-6) and press enter: " << endl;

      cout << endl;                                                            
   }

   return 0;
}

int main (){
   Menu();
	
   return 0;
}

//Chrsiterrr!!! Need natin yung input file "TXT.FILE" para macontain natin yung data. Diba kailangan maglast long.  
Topic archived. No new replies allowed.