file handling

I created this project for students to take a test and included file handling. The program works well but the problem is that when a student takes the test, the rest of the data in the file gets erased. code:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<stdio.h>
int count=0,i,n=0;
class basic
{
public:
char name[30],fname[30],mname[30],ph[20],address[100];
int cid;
int score;
char dob[15];
basic()
{
score=0;
strcpy(name,"-");
cid=0;
strcpy(dob,"-");
}
};
class info:public basic
{
int ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10;
public:
void acc();
void eco();
void comp();
void input();
void display();
void test();
void scorer();
void scored();
}d,s[100];
void info::acc()
{
cout<<"PART A: ACCOUNTS:";
cout<<"\n1.In the absence of Partnership Deed, partners can claim for";
cout<<"\n1.Salary\n2.Interest on capital";
cout<<"\n3.Interest of loans and advances\n4.Distribution of profit in their capital ratio";
cout<<"\nAns:";
cin>>ch1;
cout<<"\n\n2.If the new partner is unable to bring his/her share of goodwill, then goodwill is adjusted by";
cout<<"\n1.Debiting Goodwill Account and crediting sacrificing Partner's Capital Account\n2.Debiting Goodwill Account and crediting all Partner's Capital Account in new ratio";
cout<<"\n3.Debiting sacrificing Partners Capital Account and crediting gaining Partners Capital Account\n4.Debiting gaining Partner's Capital Account and crediting sacrificing Partner's Capital Account";
cout<<"\nAns:";
cin>>ch2;
cout<<"\n\n3.Which of the following items is recorded on the debit side of Retiring Partner’s Capital Account?";
cout<<"\n1.Interest on Capital\n2.Interest on Drawings";
cout<<"\n3.Share of General Reserve\n4.Share of credit balance of Profit and Loss Account";
cout<<"\nAns:";
cin>>ch3;
cout<<"\n\n4.Which of the following accounts is credited to transfer profit on revaluation of assets and liabilities, if the Partner’s Capital Accounts is maintained by Fixed Method?";
cout<<"\n1.Revaluation Account\n2.Partner's Capital Account";
cout<<"\n3.Partner's Current Account\n4.Profit and Loss Appropriation Account";
cout<<"\nAns:";
}
void info::eco()
{
cout<<"\n\nPART B:ECONOMICS";
cout<<"\n5.Which of these statements is correct regarding the massive unemployment?";
cout<<"\n1.It is shown by a point inside the PPC.\n2.It leads to the leftward shift of the PPC.";
cout<<"\n3.It leads to the rightward shift of the PPC.\n4.It is represented by a point lying on the PPC.";
cout<<"\nAns:";
cin>>ch5;
cout<<"\n6.Which of the following degrees of price elasticity will be depicted by a vertical demand curve which is parallel to y-axis?";
cout<<"\n1.Elastic \n2.Perfectly elastic";
cout<<"\n3.Inelastic \n4.Perfectly inelastic";
cout<<"\nAns:";
cin>>ch6;
cout<<"\n7.Which of the following curves is an inverse S-shaped curve?";
cout<<"\n1.Average Cost \n2.Marginal Cost";
cout<<"\n3.Total Variable Cost\n4.Average Fixed Cost";
cout<<"\nAns:";
cin>>ch7;
cout<<"\n8.Which of the following characteristics is not correct for the demand curve faced by a firm under perfect competition?";
cout<<"\n1.Demand curve is elastic.\n2.Demand curve is downward sloping.";
cout<<"\n3.Demand curve coincides with MR curve.\n4.Demand curve depicts the price of product";
cout<<"\nAns:";
cin>>ch8;
}
void info::comp()
{
cout<<"\n\nPART C: COMPUTERS";
cout<<"\n9.In which visibility mode are data in structures, by default?";
cout<<"\n1.Private\n2.Protected\n3.Public\n4.None of the above";
cout<<"\nAns:";
cin>>ch9;
cout<<"\n10.Name the header file to which the function rand( ) belongs?";
cout<<"\n1.#include<stdlib.h>\n2.#include<iomanip.h>";
cout<<"\n3.#include<stdio.h>\n4.#include<fstream.h>";
cout<<"\nAns:";
cin>>ch10;
}
void info::scorer()
{
ofstream f;
f.open("pripoo.txt",ios::out);
if(ch1==3)
score=score+1;
if(ch2==4)
score+=1;
if(ch3==2)
score+=1;
if(ch4==3)
score+=1;
if(ch5==1)
score+=1;
if(ch6==4)
score+=1;
if(ch7==3)
score+=1;
if(ch8==2)
score+=1;
if(ch9==3)
score+=1;
if(ch10==1)
score+=1;
f<<score;
f.close();
}
void info::scored()
{
ifstream f;
f.open("pripoo.txt",ios::in);
f>>score;
cout<<"\n\tScore:"<<score;
}
void info::input()
{
clrscr();
cout<<"\n\n\t\tEntering a new student!";
ofstream f;
f.open("pripoo.txt",ios::app);
cout<<"\n\n\tEnter details of student!";
cout<<"\n\t-------------------------\n";
cout<<"\n\tEnter Name:";
gets(name);
cout<<"\n\tEnter the student ID:";
cin>>cid;
cout<<"\n\tDate Of Birth as DD/MM/YY:";
gets(dob);
cout<<"\n\tEnter Father's Name:";
gets(fname);
cout<<"\n\tEnter Mother's Name:";
gets(mname);
cout<<"\n\tEnter telephone number with the STD code:";
gets(ph);
cout<<"\n\tEnter address:";
gets(address);
getch();
}
void info::display()
{
cout<<"\n\n\tDisplay students: ";
cout<<"\n\t-------------------\n";
cout<<"\n\n\tStudent details are:";
cout<<"\n\n\tName:";
cout<<name;
cout<<"\n\tID:";
cout<<cid;
cout<<"\n\tDate of birth:";
cout<<dob;
cout<<"\n\tFather's Name:";
cout<<fname;
cout<<"\n\tMother's Name:";
cout<<mname;
cout<<"\n\tScore:";
cout<<score;
cout<<"\n\tPhone Number:";
cout<<ph;
cout<<"\n\tResidence:";
cout<<address;
}
void info::test()
{
acc();
eco();
comp();
scorer();
clrscr();
}
void main()
{
clrscr();
f:
cout<<"\nMain Menu";
cout<<"\n1.Enter student details";
cout<<"\n2.Take the test";
cout<<"\n3.View a single student's report";
cout<<"\n4.View the report of all students";
cout<<"\n5.Exit";
cout<<"\nEnter your choice:";
int ch;
cin>>ch;
switch(ch)
{
case 1:
{
ofstream file1("pripoo.txt",ios::app);
d.input();
file1.write((char*)&d,sizeof(d));
file1.close();
goto f;
}
case 2:
{
cout<<"\nEnter the id of the student taking the test:";
ifstream fout("pripoo.txt",ios::in);
int pos;
int tid;
cin>>tid;
while(!fout.eof())
{
pos=fout.tellg();
fout.read((char*)&d,sizeof(d));
if(d.cid==tid)
{
d.test();
fout.seekg(pos);
break;
}
}
fout.close();
goto f;
}
case 3:
{
cout<<"\nEnter the ID:";
int id;
cin>>id;
ifstream fout;
fout.seekg(0);
fout.open("pripoo.txt",ios::in);
while(!fout.eof())
{
fout.read((char*)&d,sizeof(d));
if(d.cid==id)
{
d.display();
break;
}
}
fout.close();
goto f;
}
case 4:
{
ifstream fin("pripoo.txt",ios::in);
fin.seekg(0);
while(fin.read((char*)&d,sizeof(d)))
{
d.display();
}
fin.close();
goto f;
}
case 5:
{
cout<<"\nPress any key to exit";
getch();
exit(0);
}
}
}

It would be great if someone could help me with this. Thank you very much for taking time and reading this post.
closed account (G309216C)
Hi,

I did not read the code nor want to, reason being is that the code has not been put into code tags which is fairly simple to find when posting next if you are a teacher and you have no idea why it is failed I highly suggest you to quit the job because these are FILE I\O basics.

GL
Topic archived. No new replies allowed.