Nov 28, 2014

Write a Object Oriented Program to Display Student Information

/*
Create a class named as Student containing the following data members:
--Roll_no
--Name
--Marks1, Marks2, Marks3
Write necessary member functions:
1. to accept details of all students
2. to display details of one student
3. to display details of all students(Use Function overloading).

*/


#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include<cstring>
using namespace std;

    class student
    {
        public:
        char first_name[30], last_name[30];
        int roll;
        float ban_mark,eng_mark,mat_mark;
  

        void body()
        {
    system("cls");
    cout << "\n\n";
    cout << "\t\t****** INFORMATION OF STUDENTS ******";
    cout << "\n\n";
    cout << "\n \t\t 1. Add    Students";
    cout << "\n \t\t 2. Search by first Name ";
    cout << "\n \t\t 3. Show  all Records";
    cout << "\n \t\t 4. Delete Records";
    cout << "\n \t\t 5. Exit";
    cout << "\n\n";
    cout <<"\t Select What you want:<1-5> ";

        }
    void getdata()
    {
        system("cls");
        cout << "First Name         : ";
        cin >> first_name;
        cout << "Last Name          : ";
        cin >> last_name;
        cout << "Roll No            : ";
        cin >>roll;
        cout << "Marks of Bangla    : ";
        cin >> ban_mark;
        cout << "Marks of English   : ";
        cin >> eng_mark;
        cout << "Marks of Mathmetic : ";
        cin >> mat_mark;

    }

        void showdata()
        {
        cout<< "\n";
        cout <<"\t Name of Students : "<< first_name << setw(8)<< last_name;
        cout << "\n";
        cout <<"\t Roll No          : " <<roll;
        cout << "\n";
        cout <<"\t Bangla           : " <<ban_mark;
        cout << "\n";
        cout << "\t English          : " <<eng_mark;
        cout << "\n";
        cout <<"\t Mathmetics       : " <<mat_mark<<endl;
        cout<<"\t" <<endl<<"================================="<<endl;
        }


    };

int main()
{

student inf;
char xfirst_name[30];
char option, select;


    FILE *fp, *ft;
    fp=fopen("users.txt","rb+");

    if (fp == NULL)
    {
        fp = fopen("users.txt","wb+");

            return 0;

    }

    while(1)
    {
    inf.body();
    select = getche();

        switch(select)
            {
            case '1':

            fseek(fp,0,SEEK_END);
            option ='Y';
            while(option == 'Y' || option == 'y')
            {
            inf.getdata();

               fwrite(&inf,sizeof(inf),1,fp);
                cout << "\n Add Another Record (Y/N) ";
                fflush(stdin);
                option = getchar();
            }
            break;

            case '2' :
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
                cout << "\n Enter the first name of the student : ";
                cin >> xfirst_name;

                rewind(fp);
                while (fread(&inf,sizeof(inf),1,fp)==1)
                {
                    if (strcmp(inf.first_name,xfirst_name)== 0)
                    {
                 inf.showdata();

                        break;
                    }
                    else
                        cout<<endl;
                        cout<<"record not found"<<endl;
                }
                cout << "\n Find  Another Record (Y/N) ";
                fflush(stdin);
                option = getchar();
            }
            break;

            case '3':
            system("cls");
            rewind(fp);
            cout << "=== View all Records in the Database ===";
            cout << "\n";
            while (fread(&inf,sizeof(inf),1,fp))
            {
                inf.showdata();
            }
             cout << "\n\n";
            system("pause");
            break;


            case '4':
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
            cout << "\n Enter the fitst name of the student to delete : ";
            cin >> xfirst_name;

            ft = fopen("temp.dat", "wb");

                rewind(fp);
                while (fread (&inf, sizeof(inf),1,fp) == 1)

                    if (strcmp(inf.first_name,xfirst_name) != 0)
                    {
                        fwrite(&inf,sizeof(inf),1,ft);
                    }
                fclose(fp);
                fclose(ft);
                remove("users.txt");
                rename("temp.dat","users.txt");

                fp=fopen("users.txt","rb+");

                cout << "\n Delete option Record (Y/N) ";
                fflush(stdin);
                option = getchar();
            }

            break;

            case '5':
            fclose(fp);
            system("cls");
            cout << "\n\n";
            cout << "\t\t********************************************************"<<endl;
            cout << "\t\t*          THANKS BY MUJAFFOR , Batch-46, AUB          *"<<endl;
            cout << "\t\t********************************************************"<<endl;
            cout << "\n\n";
            exit(0);
        }
    }


    system("pause");
    return 0;
}

No comments:

Create a Diamond in C++

 #include<iostream> using namespace std; int main() { int i, j, k,r; cout<<"Enter the row of diamond   "; cin>>r...