A program to clarify the use of files how to open how to close

This is a c++ program made using files to generate a report of enggineering student. In this program it will take input of students full info like name, roll and marks.this program includes the concept of opening and cloasing of file. This program also includes a few extra header files and also the concept of switch case too. The program is as follows:
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>
class student
{
char name[30];
int rollno;
int marks;
public:
void input()
{
cout<<"\nEnter Name: ";
gets(name);
cout<<"Enter Rollno.: ";
cin>>rollno;
cout<<"enter marks";
cin>>marks;
}
void display()
{
cout<<"\n"<<name<<"\t"<<rollno<<"\t"<<marks<<"\t";
if(marks>=96)
cout<<"computer sc.";
else if(marks>=91&&marks<=95)
cout<<"Electronics";
else if(marks>=86&&marks<=90)
cout<<"Mechanical";
else if(marks>=81&&marks<=85)
cout<<"Electrical";
else if(marks>=76&&marks<=80)
cout<<"Chemical";
else if(marks>=71&&marks<=75)
cout<<"Civil";
else
cout<<"none";

}

};
void main()
{
clrscr();
student s;
int n,i,j;
fstream ofile,afile;

char ch,ch1;

do
{
cout<<"\n\t1.Add records\n\t2.Show Records\n\t3.Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
    case '1' :
ofile.open("st.dat",ios::app|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(student));
}
ofile.close();
break;
    case '2' : cout<<"\nName\tRollno\tMarks\tStream";
afile.open("st.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(student));
if (!afile)
break;
s.display();
}
afile.close();
break;
    case '3' : exit(0);
}
cout<<"\n\t DO U want to continue <Y/N>: ";
cin>>ch1;
  }while(tolower(ch1)!='n');
getch();

}

Share this

Related Posts

Previous
« Prev Post