Help me out!!!!

I made this program for my school holiday homework. I made a program to enter records into a binary file and then search for existing record. The problem i am having is that it searches for the first record correctly but is unable to search the other records.

CODE:

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

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
struct book
{char author[40];
int bid;
char bname[40];};


struct student
{book b;
char name[20];
int rno;
int admno;
char sec;
int cl;};
void getdata(student &);
void writefile();
void searchfile(student &,int);
void readfile();
student s1;
void main()
{clrscr();
char ch1='y';
int ch,id;
cout<<"\t\tAIR FORCE BAL BHARATI SCHOOL LIBRARY RECORDS";
cout<<"\n\n\n\t\t\t  PRESS ANY KEY TO CONTINUE";
getch();
clrscr();
cout<<"\t\t  PROGRAM TO MAINTAIN SCHOOL LIBRARY RECORDS \n\t\t\tMADE BY SOUMYA SAMANTA\n";
while ((ch1=='y')||(ch1=='Y'))
{cout<<"1)Write into a file\n2)Search for an issued book"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{case 1:
writefile();
break;
case 2:
searchfile(s1,id);
break;
case 3:
default:
cout<<"\nPlease enter a valid choice";
break;}
cout<<"\nDo you want to continue (y/n) ? : ";
cin>>ch1;
cout<<"\n\n\n";}
getch();
}

void writefile()
{clrscr();
ofstream f1;
f1.open("library.dat",ios::app|ios::binary);
getdata(s1);
f1.write((char*)&s1,sizeof(student));
f1.close();}

void getdata(student &s)
{clrscr();
cout<<"Enter name : ";
gets(s.name);
cout<<"\nEnter roll no. : ";
cin>>s.rno;
cout<<"\nEnter admission number : ";
cin>>s.admno;
cout<<"\nEnter class : ";
cin>>s.cl;
cout<<"\nEnter section : ";
cin>>s.sec;
cout<<"\nEnter book ID number : ";
cin>>s.b.bid;
cout<<"\nName of author : ";
gets(s.b.author);
cout<<"\nName of book is : ";
gets(s.b.bname);
cout<<"\nYour details have been saved ";
cout<<"\nPress any key to continue ";
getch();
clrscr();}

void searchfile(student &s1,int id)
{clrscr();
cout<<"Enter book ID to be searched for : ";
cin>>id;
ifstream f1;
char f='n';
f1.open("library.dat",ios::in|ios::binary);
while (!f1.eof())
{f1.read((char*)&s1,sizeof(student));
if (id==s1.b.bid)
f='y';
break;}
if (f=='y')
readfile();
else
cout<<"\nMatch not found";}

void readfile()
{clrscr();
ifstream f2;
f2.open("library.dat",ios::in||ios::binary);
while (!f2.eof())
{f2.read((char*)&s1,sizeof(student));
cout<<"\n\t\t\tDetail of the searched book is  \n";
cout<<"\nName of student : "<<s1.name;
cout<<"\nAdmission number is : "<<s1.admno;
cout<<"\nRoll no. is : "<<s1.rno;
cout<<"\nClass is "<<s1.cl<<"-"<<s1.sec;
cout<<"\nName of book is : "<<s1.b.bname;
cout<<"\nName of author is : "<<s1.b.author;
cout<<"\nBook ID is : "<<s1.b.bid;
break;}
f2.close();}
Could you indent please?
Your code is unreadable!
Please post formatted code next time.

The if() in 93 needs {} around f='y'; and break;.
After that you have the found book already in s1 and do not need to call readfile().
@caligulaminus: if i dont call the function readfile() how will i display the file found??
I don't really understand your question.
If you wrote the code yourself, as you stated, you should have no problem displaying the data.
Why do all your functions return void? Also, main must return an int, not void. For your Get functions wouldn't it make more sense to return the data rather than modifying outside data through a reference? And yes, please do indent. You may even be able to find a feature in your IDE or on the web to auto-indent the code for you.
Topic archived. No new replies allowed.