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();
}

>>I can compile and run the program
Not with Dev-C++ you didn't.

>>"gotoxy(15,2)";

What the hell is that!

Why did you put a function call in quotes? Answer: because you most likely plagerized this code from someone else who write it for Turbo C. You should have just deleted those lines instead of putting them in quotes.

#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
{ 

cout<<"\n XXX Secondary School Student's Union"<<"\n";
cout<<"\n 1. Add new member information";
cout<<"\n 2. Print member list";
cout<<"\n 3. Exit";
cout<<"\n\n Please enter your choice: ";
cin>>choice;

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*)&p,sizeof(p));//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)
{
cout<<"NO RECORD EXIT";
cout<<"\n\nPRESS ANY KEY...";
getch();
}
else
{
cout<<endl<<endl<<"\n\n\n\n\tPRESS ANY KEY...";
getch();
}
}
void group::exits( )
{
f.close();
}

So what are the errors your compiler makes? I suspect it doesn't like lines 1, 2, 3 or 8. The header file on line 3 is not supported by 32-bit compilers such as Dev-C++ and the other ones are just obsolete -- you should use the version without the *.h extension. Your program also doesn't need line 6, since its not using anything from stdio.h.

Edited 6 Years Ago by Ancient Dragon: n/a

I don't believe he has a return in main as well. It's hard to tell without any indenting. Also I have never done it but is it legal to call main in a function? I always just use a return statement.

Return in main is not required -- if absent the compiler will produce code to return 0.

>> is it legal to call main in a function
Yes and no. The compiler won't complain but its not supposed to be done. main() is the entry point into your program and should not be called from anywhere in your code.

This article has been dead for over six months. Start a new discussion instead.