Pages

Maintaining STUDENT records in a file - C++ Program

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.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<<"\nEnter Rollno : ";
       cin>>rollno;
       cout<<"\n Enter Marks : ";
       cin>>marks;
    }
    void display()
    {
       cout<<"\n"<<name<<"\t"<<rollno<<"\t"<<marks;
       if(marks>=96)
  cout<<"\tComputer SC";
       else if(marks>=91&&marks<90)
  cout<<"\tElectronics";
       else if(marks>=86&&marks<=90)
  cout<<"\tMechanical";
       else if(marks>=81&&marks<=85)
  cout<<"\tElectrical";
       else if(marks>=76&&marks<=80)
  cout<<"\tChemical";
       else if(marks>=71&&marks<=75)
  cout<<"\tCivil";
       else
  cout<<"\tNone";
    }
};
    void main()
    {
       clrscr();
       student s;
       int i, j, n, ch;
       fstream ofile, afile;
       char ch1;
       do
       {
   clrscr();
   cout<<"\n----MENU----";
   cout<<"\n1.Add Record\n2.Show Record"
       <<"\n3.Exit";
   cout<<"\nEnter your choice : ";
   cin>>ch;
   switch(ch)
   {
      case 1: ofile.open("ST.dat",ios::app|ios::binary);
       cout<<"\nEnter number of records to be entered : ";
       cin>>n;
       for(i=0;i<n;i++)
       {
          s.input();
          ofile.write((char *)&s,sizeof (s));
       }
       ofile.close();
       break;
      case 2: cout<<"\nName\tRollno\tMarks\tStream";
       afile.open("ST.dat",ios::in|ios::binary);
       while(afile)
       {
          afile.read((char *)&s,sizeof (s));
          if(!afile)
          break;
          s.display();
       }
       afile.close();
       break;
      case 3: exit(0);
     default: cout<<"\nWrong choice!!!";
    }
    cout<<"\nPress any key to cotinue";
    getch();
   }while(1);
        }

No comments:

Post a Comment