Pages

CPP Menu driven program for maintaining EMPLOYEE details

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
class employee
{
      int eid;
      char ename[20];
      char empaddr[30];
      char empdesig[20];
      float sal;
   public:
      void getdata();
      void putdata();
      int return_id()
      {
  return eid;
      }
}s;
ifstream fin;
ofstream fout;
void employee::getdata()
{
   cout<<"\n  Enter Employee ID : ";cin>>eid;
   cout<<"\nEnter Employee Name : ";gets(ename);
   cout<<"\n      Enter Address : ";gets(empaddr);
   cout<<"\n  Enter Designation : ";gets(empdesig);
   cout<<"\n       Enter Salary : ";cin>>sal;
}
void employee::putdata()
{
   cout<<"\n----EMPLOYEE DETAILS----";
   cout<<"\n  Employee ID = "<<eid;
   cout<<"\nEmployee Name = "<<ename;
   cout<<"\n      Address = "<<empaddr;
   cout<<"\n  Deisgnation = "<<empdesig;
   cout<<"\n       Salary = "<<sal;
}
void create()
{
   int n;
   fout.open("record.dat",ios::out|ios::binary);
   cout<<"\nEnter Number of Employees"
       <<"to be Added : ";
   cin>>n;
   for(int i=0;i<n;i++)
   {
      cout<<"\nEnter Details of Employee "<<i+1;
      cout<<"\n-----------------------------";
      s.getdata();
      fout.write((char *)&s,sizeof(s));
      cout<<"\nDetails are Added to File Successfully";
   }
   fout.close();
}
void append()
{
   fout.open("record.dat",ios::app|ios::binary);
   cout<<"\n---Enter the Details to be Appended---";
   s.getdata();
   fout.write((char *)&s,sizeof(s));
   fout.close();
}
void display()
{
   fin.open("record.dat",ios::in|ios::binary);
   while(fin.read((char *)&s,sizeof(employee)))
      if(!fin.eof()) s.putdata();
   fin.close();
}
void modify()
{
   int flag=0;
   fstream f;
   f.open("record.dat",ios::in|ios::out|ios::binary);
   long pos;
   int g;
   employee s;
   cout<<"\nEnter the ID to modify : ";cin>>g;
   while(f)
   {
      pos=f.tellg();
      f.read((char *)&s,sizeof(s));
      if(s.return_id()==g)
      {
  s.getdata();
  flag=1;
  f.seekg(pos);
  f.write((char *)&s,sizeof(s));
      }
   }
   if(flag==0)
   {
      cout<<"\nSorry ID not found";
   }
   f.close();
}
void main()
{
   int c;
   do
   {
      clrscr();
      cout<<"\n----MENU----";
      cout<<"\n1.Create\n2.Display\n3.Append"
   <<"\n4.Modify\n5.Exit";
      cout<<"\nEnter your choice : ";cin>>c;
      switch(c)
      {
  case 1: create();
   break;
  case 2: display();
   break;
  case 3: append();
   cout<<"\nNew Details are added";
   break;
  case 4: modify();
   break;
  case 5: exit(0);
 default: cout<<"\nWrong choice!!!";
       }
       cout<<"\nPress any key to continue";
       getch();
      }while(1);
}


CPP Program for mantaining BOOK Details

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class book
{
      int bno;
      char bname[30];
      int price;
   public:
   void input()
   {  cout<<"\nEnter Book Number : ";
      cin>>bno;
      cout<<"\nEnter Book Name   : ";
      gets(bname);
      cout<<"\nEnter the Price   : ";
      cin>>price;
   }
   void display()
   {
      cout<<"\nBook Number : "<<bno;
      cout<<"\nBook Name   : "<<bname;
      cout<<"\nBook Price  : Rs"<<price;
   }
   int getbno()
   {
      return bno;
   }
};
void main()
{
   book b;
   int n, i, j, choice;
   fstream file;
   char ch;
   do
   {
      clrscr();
      cout<<"\n----MENU----";
      cout<<"\n1.Enter Details of Book\n2.Display the Details"
   <<"\n3.Search for Book\n4.Exit";
      cout<<"\nEnter your choice : ";
      cin>>choice;
      switch(choice)
      {
  case 1: file.open("bk.dat",ios::out|ios::binary);
   cout<<"\nEnter Number of Records to be Stored : ";
   cin>>n;
   for(i=0;i<n;i++)
   {
       b.input();
       file.write((char *)&b,sizeof(b));
   }
   file.close();
   break;
  case 2: file.open("bk.dat",ios::in|ios::binary);
   cout<<"\nThe Details are ";
   cout<<"\n---------------";
   do
   {
      file.read((char *)&b,sizeof(b));
      if(!file.eof()) b.display();
      cout<<"\n";
   }while(file);
   file.close();
   break;

  case 3: int bn,flag=0;
   cout<<"\nEnter the Book Number to be Searched : ";
   cin>>bn;
   file.open("bk.dat",ios::in|ios::binary);
   while(file)
   {
      file.read((char *)&b,sizeof(b));
      if(bn==b.getbno())
      {
         b.display();
         flag=1;
         break;
      }
   }
   if(flag==0)
      cout<<"\nNo Recors Found";
   file.close();
   break;
  case 4: exit(0);
 default: cout<<"\nWrong choice!!!";
 }
 cout<<"\nPress any key to continue";
 getch();
       }while(1);
}

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

Menu driven C++ program for writing, reading a file and writing vowels in another file

#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<ctype.h>
void main()
{
   clrscr();
   int n,choice, flag;
   fstream file1,file2;
   char str[256];
   char ch;
   do
   {
      clrscr();
      cout<<"\n---MENU---\n1.Write text to the first file"
   <<"\n2.Read text from first file\n3.Write vowels to the second file"
   <<"\n4.Display the second file\n5.Exit";
      cout<<"\nEnter your choice : ";
      cin>>choice;
      switch(choice)
      {
  case 1: file1.open("smp1.txt",ios::out);
   cout<<"\nEnter the text : ";
   gets(str);
   file1<<str;
   file1.close();
   cout<<"\nFile1 Created successfully";
   break;
  case 2: file1.open("smp1.txt",ios::in);
   while(file1)
   {
      file1.get(ch);
      cout<<ch;
   }
   file1.close();
   break;
  case 3: file1.open("smp1.txt",ios::in);
   file2.open("smp2.txt",ios::out);
   while(file1.eof()==0)
   {
      file1.get(ch);
      if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
         file2.put(ch);
   }
   file1.close();
   file2.close();
   cout<<"\nFile 2 is created with vowels from File 1";
   break;
  case 4: file2.open("smp2.txt",ios::in);
   cout<<"\nLower case vowels : ";
   flag=0;
   while(file2)
   {
      file2.get(ch);
      if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
      {
         cout<<ch;
         flag=1;
      }
   }
   if(flag==0)
      cout<<"\nNo lower case vowels";
   cout<<"\nUpper case vowels : ";
   flag=0;
   file2.close();
   file2.open("smp2.txt",ios::in);
   while(file2)
   {
      file2.get(ch);
      if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
         {
     cout<<ch;
     flag=1;
         }
   }
   if(flag==0)
      cout<<"\nNo upper case vowels";
   break;
  case 5: exit(0);
 default: cout<<"\nWrong choice!!!";
   break;
      }
      cout<<"\nPress any key to continue";
      getch();
     } while(1);
    }


Program for printing the Upper and Lower triangular matrices of a square matrix (for Plus2 Students)

#include<iostream.h>
#include<conio.h>
#include<process.h>
void l_triangle(int a[30][30], int n)
{
   int i=0, j=0;
   for(i=0;i<n;i++)
   {
     for(j=0;j<n;j++)
     {
       if(i>=j)
	 cout<<a[i][j]<<" ";
       else
       cout<<" ";
     }
     cout<<"\n";
   }
}
void u_triangle(int a[30][30], int n)
{
   int i=0, j=0;
   for(i=0;i<n;i++)
   {
     for(j=0;j<n;j++)
     {
       if(i<=j)
	 cout<<a[i][j]<<" ";
       else
	 cout<<" ";
     }
     cout<<"\n";
   }
}
void main()
{
   clrscr();
   int i, j, a[30][30], r, c; char ch;
   do
   {
     cout<<"\n\nMENU\n\n";
     cout<<"1. Enter elements\n2. Display matrix"
	 <<"\n3. Display upper triangle\n4. Display lower triangle"
	 <<"\n5. Exit";
     cout<<"\nEnter your choice ";
     cin>>c;
     switch(c)
     {
       case 1: cout<<"\nEnter row and column(square matrix only)";
	       cin>>r>>c;
	       cout<<"\nEnter the elements";
	       for(i=0;i<r;i++)
		 {for(j=0;j<c;j++)
		   cin>>a[i][j];
		 }
	       break;
       case 2: cout<<"\nMatrix is";
	       for(i=0;i<r;i++)
	       {
		 cout<<"\n";
		 for(j=0;j<r;j++)
		 cout<<a[i][j]<<" ";
	       }
	       break;
       case 3: cout<<"\nUpper triangle matrix\n";
	       u_triangle(a,r);
	       break;
       case 4: cout<<"\nLower triangle matrix\n";
	       l_triangle(a,r);
	       break;
       case 5: cout<<"\nExit";
	       exit(0);
      default: cout<<"\nWrong choice";
     }
   cout<<"\nDo you want to continue";
   cin>>ch;
   }while( ch=='Y'|| ch=='y');
   getch();
}