I can compile and run the program, but cannot output the text file and print the member list result. Anyone can advice the solution? Thanks.
1. Add new member information
2. Print member list
3. Exit
#include<iostream.h>
#include<fstream.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
struct student
{
char Stdid[7];
char Stdname[30];
char Class[3];
char Position[30];
}p;
class group
{
public:
fstream f;
group();
void addrec();
void listrec();
void exits();
};
int main( )
{
int choice;
char v;
"textcolor(YELLOW)";
group g;
do
{
"gotoxy(15,2)";
cout<<"\n XXX Secondary School Student's Union"<<"\n";
"gotoxy(12,12)";
cout<<"\n 1. Add new member information";
"gotoxy(12,14)";
cout<<"\n 2. Print member list";
"gotoxy(12,16)";
cout<<"\n 3. Exit";
"gotoxy(12,25)";
cout<<"\n\n Please enter your choice: ";
cin>>choice;
"clrscr()";
switch(choice)
{
case 1:
g.addrec();
break;
case 2:
g.listrec();
break;
case 3:
g.exits();
exit(0);
default:
cout<<"\nPRESS THE SPECIFIED KEYS ONLY";
"delay(1500)";
break;
}
}
while(choice!=0);
}
group::group()
{
f.open("students.txt",ios::binary|ios::in|ios::out);
if(!f)
{
exits();
}
f.write((char*)&"temp",sizeof(student));//writes an the end of file, dont over-write previous data.
f.close();
}
void group::addrec( )
{
char ch;
f.seekp(0L,ios::end);
do
{
cout<<"\n<<TO RETURN THE MAIN MENU PRESS 'R' ADD INFORMATION PRESS 'N' >>:";
cin>>ch;
if(ch=='r'||ch=='R')
main();
cout<<"\nPlease enter the followings:"<<"\n";
cout<<"\nStudent Id:";
cin>>p.Stdid;
cout<<"\nStudent Name:";
cin>>p.Stdname;
cout<<"\nClass :";
cin>>p.Class;
cout<<"\nPostion:";
cin>>p.Position;
f.write((char*)&p,sizeof(p));
cout<<"\nAAre you sure to add new member information?(Y/N):";
cin>>ch;
cout<<"\n";
}
while(ch=='y'||ch=='Y');
}
void group::listrec()
{
int j=1,a,c=0;
f.seekg(0L,ios::beg);
cout<<"\nPrint the member list as followings:";
cout<<"\n\n\n\n "<<"Student Id"<<" "<<" Student Name"<<" "<<"Class"<<" "<<"Postion"<<"";
cout<<"\n\t ---------------------------------------";
while(f.read((char*)&p,sizeof(p)))
{
cout<<"\n";
{
cout<<endl<<"RECORD NO";"<<j++<<setw(8)<<p.Stdid<<setw(14)<<p.Stdname<<setw(9)<<p.Class<<setw(12)<<p.Postion";
c++;
}
}
f.clear();
if(c==0)
{
"gotoxy(10,10)";
cout<<"NO RECORD EXIT";
"gotoxy(10,12)";
cout<<"\n\nPRESS ANY KEY...";
getch();
}
else
{
cout<<endl<<endl<<"\n\n\n\n\tPRESS ANY KEY...";
getch();
}
}
void group::exits( )
{
f.close();
}