View difference between Paste ID: LarwkdcL and 63WPirPe
SHOW: | | - or go back to the newest paste.
1
//=========================================================
2
// File Name    : headr.h
3
// Project Name : Phone book
4
// Version      : v0.0.2
5
// Copyright    : you are free to copy , share and use this code
6
// Description  : Phone book program, using POD types , and Binary file
7
// Compiler     : Gcc MingW
8
// IDE          : Code::blocks
9
//===========================================================
10-
//i do not comment my code ...it  was HARD to write ..so it is HARD to read...
10+
11
/*
12
main.cpp
13
#include"headr.h"
14
int main()
15
{
16
17
    gui();
18
    return 0;
19
}
20
*/
21
#include<iostream>
22
#include<stdio.h>
23
#include<cstdlib>
24
#include<process.h>
25
#include<string.h>
26
#include<fstream>
27
using namespace std;
28
char nul[1]={""};
29
char na[10]={"NA"};
30
class Personal_Info
31
{
32
public:
33
    char name[100];
34
    char mobile[15];
35
    char email[15];
36
public:
37
    Personal_Info()
38
    {
39
        strcpy(name,"NA");
40
        strcpy(mobile,"NA");
41
        strcpy(email,"NA");
42
    }
43
    void take_name();
44
    void take_mobile();
45
    void take_email();
46
    void show_full_info();
47
    void validate_mobile();
48
    void validate_name();
49
    void validate_email();
50
    int check(char field[]);
51
};
52
int Personal_Info::check(char field[])
53
{
54
    return strcmp(name,field);
55
}
56
void Personal_Info::validate_name()
57
{
58
    if(strcmp(name,nul)==0)
59
       {
60
            cout<<"\n..NAME NOT GIVEN...\n";
61
            cin.ignore();
62
            strcpy(name,na);
63
            take_name();
64
       }
65
}
66
void Personal_Info::validate_mobile()
67
{
68
    if(strcmp(mobile,nul)==0)
69
    {
70
        cout<<"\n..MOBILE NO NOT GIVEN>>\n";
71
        strcpy(mobile,na);
72
        take_mobile();
73
    }
74
}
75
void Personal_Info::validate_email()
76
{
77
    if(strcmp(email,nul)==0)
78
    {
79
        cout<<"\n..EMAIL NOT GIVEN..\n";
80
        strcpy(email,nul);
81
    }
82
}
83
void Personal_Info::show_full_info()
84
{
85
    cout<<"\n***** DISPLAYING DETALIS ****\nS";
86
    cout<<"\nName      :"<<name;
87
    cout<<"\nMobile    :"<<mobile;
88
    cout<<"\nEmail     :"<<email;
89
}
90
void Personal_Info::take_name()
91
{
92
    cout<<"\nEnter the name ->";
93
    cin.ignore();
94
    gets(name);
95
    validate_name();
96
}
97
void Personal_Info::take_mobile()
98
{
99
    cout<<"\nEnter the mobile number ->";
100
    cin.ignore();
101
    gets(mobile);
102
    validate_mobile();
103
}
104
void Personal_Info::take_email()
105
{
106
    cout<<"\nEnter the E-mail  ->";
107
    cin.ignore();
108
    gets(email);
109
    validate_email();
110
}
111
class Phonebook
112
{
113
	private:
114
		 void save_info();
115
	protected:
116
		Personal_Info contact;
117
	public:
118
		void search_name();
119
		void delete_data();
120
		void show_all();
121
		void get_data();
122
};
123
void Phonebook::save_info()
124
{
125
    ofstream filout;
126
	filout.open("data.bin",ios::out|ios::app|ios::ate);
127
	int ch;
128
129
	if(!filout)
130
	{
131
		cout<<"\n cannot open file \n";
132
	}
133
	cout<<"\n do you want to save this info (1. yes / 2. no) ?\n";
134
	cin>>ch;
135
	if(ch==1)
136
    {
137
        filout.write((char*)&contact,sizeof(contact));
138
    }
139
	else if(ch==2)
140
		cout<<"\n info discarded \n";
141
	filout.close();
142
}
143
void Phonebook::get_data()
144
{
145
	contact.take_name();
146
	contact.take_mobile();
147
    contact.take_email();
148
	contact.show_full_info();
149
	save_info();
150
}
151
void Phonebook::delete_data()
152
{
153
154
}
155
void Phonebook::show_all()
156
{
157
	ifstream filout;
158
	filout.open("data.bin");
159
	filout.seekg(0,ios::beg);
160
	cout<<"\n******* Displaying details ******\n";
161
	if(!filout.good())
162
        cout<<"\n*** FILE IS DOES NOT EXIST ***\n";
163
    if(filout.good())
164
    {
165
        while(!filout.eof())
166
        {
167
            filout.read((char*)&contact,sizeof(contact));
168
            contact.show_full_info();
169
        }
170
    }
171
}
172
void Phonebook::search_name()
173
{
174
	int token;
175
	char field[100];
176
	cout<<"Enter the first name of the person you wanna search ?? \n";
177
	cin.ignore();
178
	gets(field);
179
	cout<<"person to be searched is "<<field;
180
    cin.get();
181
    token=0;
182
	ifstream readfil;
183
	readfil.open("data.bin");
184
	if(!readfil.good())
185
        cout<<"\n*** FILE IS DOES NOT EXIST ***\n";
186
    if(readfil.good())
187
    {
188
        while(!readfil.eof())
189
        {
190
            readfil.read((char*)&contact,sizeof(contact));
191
            if((contact.check(field))==1)
192
            {
193
                token=1;
194
                contact.show_full_info();
195
            }
196
        }
197
        if(token==0)
198
        {
199
            cout<<"\n*** PERSON NOT FOUND ***\n";
200
201
        }
202
    }
203
}
204
void gui()
205
{
206
    int ch;
207
    Phonebook p;
208
    while(1)
209
        {
210
            cout<<"*********PHONE BOOK**********\n";
211
            cout<<"| 1) Add new record         |\n";
212
            cout<<"| 2) Display All Records    |\n";
213
            cout<<"| 3) Search Person Name     |\n";
214
            cout<<"| 4) Delete record          |\n";
215
            cout<<"| 5) Exit                   |\n";
216
            cout<<"*****************************\n";
217
            cout<<"Enter your desired operation : ";
218
            cin>>ch;
219
            switch(ch)
220
                {
221
                    case 1:
222
                        p.get_data();
223
                        break;
224
                    case 2:
225
                        p.show_all();
226
                        break;
227
                    case 3:
228
                        p.search_name();
229
                        break;
230
                    case 4:break;
231
                    case 5:exit(1);
232
                    default:cout<<"\n Enter a valid choice";
233
                }
234
        }
235
}