C++ Program for Student Database using File handling

By | 04.01.2017

Student Database using File handling


Write a C++ Menu Driven Program to perform Add,Modify,Append and Display in File Handling. Here’s simple Program for Student Database using File handling  in C++ Programming Language.


Below is the source code for C++ Program for Student Database using File handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program for Student Database using File handling */

#include <iostream>
#include <fstream>
using namespace std;

static int totrec=0;

int main()
{
    int choice;
    while(1)
    {
        cout<<"\nChoose your choice :: \n";
        cout<<"1) Add New Record\n";
        cout<<"2) Append New records\n";
        cout<<"3) Modifying or Append new records\n";
        cout<<"4) Display records\n";
        cout<<"5) Exit\n";
        cout<<"\nEnter your choice :: ";
        cin>>choice;

        switch (choice)
        {
          case 1 :
                {
                     ofstream outfile;
                     outfile.open("C:\\Users\\acer\\Documents\\file4.txt",ios::out);
                     cout<<"\n\nPlease Enter Details :: \n";
                     cout<<"\nEnter the name :: ";
                     char name[20];
                     cin>>name;
                     outfile<<name<<endl;
                     cout<<"\nEnter Age :: ";
                     int age;
                     cin>>age;
                     outfile<<age<<endl;
                     cout<<"\nEnter programming language known :: ";
                     char lang[25];
                     cin>>lang;
                     outfile<<lang<<endl;
                     totrec= totrec + 1;
                     outfile.close();
                }
                 break;

          case 2 :
                {
                     ofstream outfile;
                     outfile.open("C:\\Users\\acer\\Documents\\file4.txt",ios::app);
                     cout<<"\n\nPlease Enter Details :: \n";
                     cout<<"\nEnter the name :: ";
                     char name[20];
                     cin>>name;
                     outfile<<name<<endl;
                     cout<<"\nEnter Age :: ";
                     int age;
                     cin>>age;
                     outfile<<age<<endl;
                     cout<<"\nEnter programming language known :: ";
                     char lang[25];
                     cin>>lang;
                     outfile<<lang<<endl;
                     totrec= totrec + 1;
                     outfile.close();
                 }
                 break;

          case 3 :
                {
                     ofstream outfile;
                     outfile.open("C:\\Users\\acer\\Documents\\file4.txt",ios::ate);
                     cout<<"\nWant to add new record ::\nEnter y or n ? ";
                     char ans;
                     cin>>ans;
                     if(ans=='y' || ans=='Y')
                     {
                         cout<<"\n\nPlease Enter Details :: \n";
                         cout<<"\nEnter the name :: ";
                         char name[20];
                         cin>>name;
                         outfile<<name<<endl;
                         cout<<"\nEnter Age :: ";
                         int age;
                         cin>>age;
                         outfile<<age<<endl;
                         cout<<"\nEnter programming language known :: ";
                         char lang[25];
                         cin>>lang;
                         outfile<<lang<<endl;
                         totrec= totrec + 1;
                     }
                     outfile.close();
                }
                 break;

          case 4 :
                {
                     ifstream infile;
                     infile.open("C:\\Users\\acer\\Documents\\file4.txt",ios::in);
                     const int size=80;
                     char line[size];
                     int counter=totrec;
                     while(counter > 0)
                     {
                     infile.getline(line,size);
                     cout<<"\n\nNAME     : "<<line<<endl;
                     infile.getline(line,size);
                     cout<<"AGE      : "<<line<<endl;
                     infile.getline(line,size);
                     cout<<"LANGUAGE : "<<line<<endl;
                     counter--;
                     }
                     infile.close();
                }
                    break;

         case 5  :
                exit(0);

          default :
                cout<<"\nInvalid Choice\nTRY AGAIN.......\n";
          }
    }
    return 0;
}

OUTPUT : :


/*  C++ Program for Student Database using File handling */

Choose your choice ::
1) Add New Record
2) Append New records
3) Modifying or Append new records
4) Display records
5) Exit

Enter your choice :: 1


Please Enter Details ::

Enter the name :: CodezClub

Enter Age :: 19

Enter programming language known :: C

Choose your choice ::
1) Add New Record
2) Append New records
3) Modifying or Append new records
4) Display records
5) Exit

Enter your choice :: 2


Please Enter Details ::

Enter the name :: John

Enter Age :: 20

Enter programming language known :: C++

Choose your choice ::
1) Add New Record
2) Append New records
3) Modifying or Append new records
4) Display records
5) Exit

Enter your choice :: 2


Please Enter Details ::

Enter the name :: Max

Enter Age :: 21

Enter programming language known :: Java

Choose your choice ::
1) Add New Record
2) Append New records
3) Modifying or Append new records
4) Display records
5) Exit

Enter your choice :: 4


NAME     : CodezClub
AGE      : 19
LANGUAGE : C


NAME     : John
AGE      : 20
LANGUAGE : C++


NAME     : Max
AGE      : 21
LANGUAGE : Java

Choose your choice ::
1) Add New Record
2) Append New records
3) Modifying or Append new records
4) Display records
5) Exit

Enter your choice :: 5

Process returned 0

Above is the source code for C++ Program for Student Database using File handling which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.5 2 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
muhammad shahid

case 4 :
{
ifstream infile;
infile.open(“C:\\Users\\acer\\Documents\\file4.txt”,ios::in);
const int size=80;
char line[size];
int counter=totrec;
while(counter > 0)
{
infile.getline(line,size);
cout<<"\n\nNAME : "<<line<<endl;
infile.getline(line,size);
cout<<"AGE : "<<line<<endl;
infile.getline(line,size);
cout<<"LANGUAGE : "<<line<<endl;
counter–;
}
infile.close();
}
break;

code not displaying outcome

muhammad shahid

#include #include using namespace std; static int totrec=0; int main() { int choice; while(1) { cout<<"\nChoose your choice :: \n"; cout<<"1) Add New Record\n"; cout<<"2) Append New records\n"; cout<<"3) Modifying or Append new records\n"; cout<<"4) Display records\n"; cout<<"5) Exit\n"; cout<>choice; switch (choice) { case 1 : { ofstream outfile; outfile.open(“C:\\Users\\acer\\Documents\\file4.txt”,ios::out); cout<<"\n\nPlease Enter Details :: \n"; cout<>name; outfile<<name<<endl; cout<>age; outfile<<age<<endl; cout<>lang; outfile<<lang<<endl; totrec= totrec + 1; outfile.close(); } break; case 2 : { ofstream outfile; outfile.open("C:\\Users\\acer\\Documents\\file4.txt",ios::app); cout<<"\n\nPlease Enter Details :: \n"; cout<>name; outfile<<name<<endl; cout<>age; outfile<<age<<endl; cout<>lang; outfile<<lang<<endl; totrec= totrec + 1; outfile.close(); } break; case 3 : { ofstream outfile; outfile.open("C:\\Users\\acer\\Documents\\file4.txt",ios::ate);… Read more »