Database application using STL(Standard Template Library) functions

Program Description

The program shows an implementation of a student database using the STL library functions.
The student class keeps track of all of a student's personal data.
For each new student, a new student object is instantiated and vectors are used to store them.
Vectors are just like arrays with contiguous memory locations and data can be accessed efficiently as in arrays.
However unlike arrays,vectors can dynamically expand in size which is automatically handeled by the storage container class.
The database program here, does not perform input validation. All inputs are assumed valid.

Program
/***********************************************************************
*
*  Database application using STL(Standard Template Library) functions
*
*  code.cheraus.com
*
************************************************************************/

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>

using namespace std;

class student 
{
 public: 
   string name;
   int rollno;
   string ph;
   string dob;

   student()
   {
      rollno=0;
   }

   void put_data()
   {
      cout<<"Enter name: ";
      cin>>name;
      cout<<"Rollno: ";
      cin>>rollno;
      cout<<"Phone: ";
      cin>>ph;
      cout<<"Enter DOB(d/m/y): ";
      cin>>dob;
   }

   void get_data()
   {
     cout<<setw(26)<<name;
     cout<<setw(25)<<rollno;
     cout<<setw(25)<<ph;
     cout<<setw(25)<<dob;
     cout<<"\n";
   }
   
   bool operator==(student a)			//connected function for find "==" sign used
   {
     if(rollno==a.rollno)
      return true;
     else 
      return false;
   }
 
   bool operator()(student a,student b)     //used for sort function when passing object
  {
       if(a.rollno < b.rollno)
         return true;
        else 
          return false;       
  }
};
   

int main()
{
   vector<student> a;  
 
   vector<student>::iterator p;
   student s;
   int choice=0;
   int i=0;
   int roll=0;
   int found=0;



do{
   cout<<"\n1.Insert\n2.Print\n3.Delete\n4.Search\n5.Sort\n6.Exit\nYour choice: ";
   cin>>choice;
  
   switch(choice) 
   {
      case 1:  s.put_data(); 
               a.push_back(s);
               cout<<"\nData added\n\n";
               break;
      case 2: if(!a.empty())
               { i=0;
                 system("clear");
                 cout<<"\n**************** Student Records ****************************\n\n"; 
                 
                 cout<<"SrNo."<<setw(5)<<setw(25)<<"Name"<<setw(25)<<"Roll No"<<setw(25)<<"Phone"<<setw(25)<<"DOB"<<"\n";

                 for(p=a.begin();p!=a.end();p++)
                  {
                    i++;
                    cout<<setw(4)<<i;
                    p->get_data();
                  }
               }
                else
                  cout<<"\nDatabase empty\n"; 
                  break;
      case 3:    cout<<"Enter roll no to delete: ";
                 cin>>roll;
                 if(!a.empty())
                 {
                   for(p=a.begin();p!=a.end();p++)
                   {  
                      found=0;
                      if(p->rollno==roll)
                       { 
                         a.erase(p);
                         found=1;
                         cout<<"\nRecord deleted\n";
                         break;
                        }
                   }
                    if(found==0)
                      cout<<"\nRecord does not exist in the database\n";
                  }
                 
                   else
                     cout<<"\nDatabase empty\n";
                 break;
     
       case 4:  cout<<"Enter roll no to search: ";
                cin>>roll;
                s.rollno=roll;  
                p=find(a.begin(),a.end(),s); 
                 if(p!=a.end())
                  {
                     cout<<"\nRecord found:\n\n"; 
                     p->get_data();
					
                   }
                 else
                  {
                    cout<<"\nRecord not found\n";
                  }
                
                  
                 break;
 
       case 5:   sort(a.begin(),a.end(),s);  
                 cout<<"\nSorted the records as per the roll numbers\n";
 	         break;
   
        case 6:   break;
   
        default:  cout<<"Invalid choice.........Try again\n";
                  break;

     }
 
  }while(choice!=6);

return 0;

}

Output
1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 1
Enter name: abc
Rollno: 1
Phone: 1234567890
Enter DOB(d/m/y): 1/1/2001

Data added


1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 1
Enter name: xyz
Rollno: 2
Phone: 4331567890
Enter DOB(d/m/y): 2/06/2005

Data added


1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 2


**************** Student Records ****************************

SrNo.         Name         Roll No         Phone                     DOB
1                 abc               1               1234567890         1/1/2001
2                 xyz               2               4331567890         2/06/2005

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit

Your choice: 1
Enter name: pqr
Rollno: 3
Phone: 9876543210
Enter DOB(d/m/y): 12/12/2004

Data added


1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 2











**************** Student Records ****************************

SrNo.         Name         Roll No         Phone                     DOB
1                 abc               1               1234567890         1/1/2001
2                 xyz               2               4331567890         2/06/2005
3                 pqr               3               9876543210         12/12/2004

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit

Your choice: 5

Sorted the records as per the roll numbers

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 4
Enter roll no to search: 6

Record not found

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 4
Enter roll no to search: 1

Record found:
abc 1 1234567890 1/1/2001


1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 2


**************** Student Records ****************************

SrNo.         Name         Roll No         Phone                     DOB
1                 abc               1               1234567890         1/1/2001
2                 xyz               2               4331567890         2/06/2005
3                 pqr               3               9876543210         12/12/2004

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit

Your choice: 3
Enter roll no to delete: 5

Record does not exist in the database

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 3
Enter roll no to delete: 1

Record deleted

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 2


SrNo.         Name         Roll No         Phone                     DOB
2                 xyz               2               4331567890         2/06/2005
3                 pqr               3               9876543210         12/12/2004

1.Insert
2.Print
3.Delete
4.Search
5.Sort
6.Exit
Your choice: 6

ΓΏ

Comments
comments powered by Disqus