name search not functioning in filestream

ok, here is the new code i made
there is a problem here, i cant seem to display a particular information block of a person in the search area,

here is the code:
some one help me please
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
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include<string.h>
#include<iomanip.h>

class voter_details
{
	private:
	struct person
	{	
		char name[50];
		char location[20];
		int age;
		char date[20];
	}p;

	fstream inFile;

	public:
	voter_details();
	
	void addrec();
	void delrec();
	void updaterec();
	void printrec();
	void show();
	void exit();
};

///////////////////////////////////////////////////////////////////////////////////
void main()
{
char choice;
voter_details g;

do
{
	
cout<<"*****************Voter's Registration*****************"<<endl;
cout<<"1. Registration"<<endl;
cout<<"2. Print all Records"<<endl;
cout<<"3. Edit"<<endl;
cout<<"4. Delete"<<endl;
cout<<"5. About"<<endl;
cout<<"6. View"<<endl;
cout<<"7. Exit"<<endl;
cout<<"******************************************************"<<endl<<endl;
	
cin>>choice;

switch(choice)
{
case '1':
g.addrec();
break;
case '2':
g.printrec();
break;
case '3':
g.updaterec();
break;
case '4':
g.delrec();
break;
case '6':
g.show();
case '0':
g.exit();
exit(0);
}
}while(choice!=0);
}

voter_details::voter_details()
//Zero Argument Constructor
{
inFile.open("vote.txt",ios::binary|ios::in|ios::out);
if(!inFile)
{
cout<<endl<<"Unable to open file";
exit();
}
}
////////////////////////////////////////////////////////////////////////////////////////
void voter_details::addrec()
{
char ch;

inFile.seekp(0L,ios::end);

do
{
		cout<<endl;
		cout<<"Enter Name: ";
		cin>>p.name;
		cout<<"Enter address: ";
		cin>>p.location;	
		cout<<"Enter age: ";
		cin>>p.age;
		cout<<"Enter Birthdate: ";
		cin>>p.date;
		cout<<endl;
inFile.write((char*)&p,sizeof(p));

cout<<"Add another Record?(Y/N)";
cin>>ch;

}while(ch=='y'||ch=='Y');

}
//////////////////////////////////////////////////////////////////////////////////////////////
void voter_details::printrec()
{
int j=1;

inFile.seekg(0L,ios::beg);//Get pointer at the beginning of the file

while(inFile.read((char*)&p,sizeof(p)))
{                                      //as long as end of file is not reached read each record and display it

	cout<<endl<<"Record #"<<j++                       //Post-increment of j
	<<endl<<"\t\tName: "<<p.name
	<<endl<<"\t\tLocation: "<<p.location
	<<endl<<"\t\tAge: "<<p.age<<endl<<"\t\tBirthdate: "<<p.date;
}

inFile.clear();

if(j==1) cout<<"No record"<<endl;               //If the address book is empty

cout<<endl<<"Press any key...."<<endl;
getch();

}
//////////////////////////////////////////////////////////////////////////////
void voter_details::updaterec()
{
char name[20];
int count=0;
long int pos;

cout<<"Enter person's name : ";
cin>>name;

inFile.seekg(0L,ios::beg);

while(inFile.read((char*)&p,sizeof(p)))
{
	if(strcmp(p.name,name)==0)
	{
		cout<<endl<<"Enter new record"<<endl;
		cout<<"Enter Name: ";
		cin>>p.name;
		cout<<"Enter address: ";
		cin>>p.location;
		cout<<"Enter age: ";
		cin>>p.age;
		cout<<"Enter Birthdate: ";
		cin>>p.date;

		pos=count*sizeof(p);

		inFile.seekp(pos,ios::beg);
		inFile.write((char*)&p,sizeof(p));
	return;
	}
count++;
}

cout<<endl<<"No Record with name = "<<name;
cout<<endl<<"Press any key...."<<endl;
getch();

inFile.clear();
}
////////////////////////////////////////////////////////////////////////////////////////
void voter_details::delrec()
{
char name[20],ch;
cout<<"Enter the name of the person to delete : "<<endl;
cin>>name;

ofstream outfile; //creates a temporary file of stream outfile
outfile.open("temporary.txt",ios::out);

inFile.seekg(0L,ios::beg); //position get pointer at the beginning of the file

while(inFile.read((char*)&p,sizeof(p)))
{
	if(strcmp(p.name,name)==0)
	{
		cout<<endl<<p.name
		<<endl<<p.location
		<<endl<<p.age
		<<endl<<p.date;
		cout<<"Delete Record? (Y/N): ";
		cin>>ch;
		
	}
	if(ch=='Y'||ch=='y')
	{
		inFile.seekg(0L,ios::beg);
		while(inFile.read((char*)&p,sizeof(p)))
		{
			if(strcmp(p.name,name)!=0)
				outfile.write((char*)&p,sizeof(p));
		}
		cout<<endl<<"Record Deleted"<<endl;
		outfile.close();
		inFile.close();

		remove("vote.txt");
		rename("temporary.txt","vote.txt");

		inFile.open("vote.txt",ios::binary|ios::in|ios::out);
	}
}
	if (ch=='N'||ch=='n')
	{
		cout<<endl<<"Deletion Cancelled"<<endl;
	outfile.close();
	}
return;
}

//////////////////////////////////////////////////////////////////////////////////////////////
void voter_details::show()
{
	ifstream inFile;
	char name[20];
	
	inFile.open("vote.txt");
	if(inFile.fail())
	{
	  cout<<" \n FILE NOT FOUND ";
	  getch();
	  return;
	}
	cout<<" \n ENTER name TO BE SEARCHED : ";
      
      cin>>name;
      
	  inFile.read((char*)&p,sizeof(p));
	  if(name==p.name)
	    {
		  cout<<p.name<<endl<<p.age<<endl<<p.location;
	      return;
	    }
	
      if(inFile.eof())
	{
	  cout<<" \n RECORD NOT FOUND ";
	}

}


void voter_details::exit()
{
inFile.close();
}
Last edited on
Topic archived. No new replies allowed.