C++ exercises and solutions-file stream


C++ File Stream


Exercise: Write a C++ program to keep records and perform statistical analysis for a class of 20 students. The information of each student contains ID, Name, Sex, quizzes Scores (2 quizzes per semester), mid-term score, final score, and total score.

The program will prompt the user to choose the operation of records from a menu as shown below:

===================================================

                                                MENU

===================================================

                                   1.     Add student records

                                   2.     Delete student records

                                   3.     Update student records

                                   4.     View all student records

                                   5.     Calculate an average of a selected student’s scores

                                   6.     Calculate total scores of a selected student

                                   7.     Display the highest and lowest scores

                                   8.     Sort students’ records by ID

                                   9.  Sort students' records by total score

Enter your choice:1

 

Note: All students records store in a file stream

C++ code solution:

#include <cstdlib>

#include <iostream>

#include<iomanip.h>

#include<fstream.h>

using namespace std;

 

struct student

{

 

int stnumber;

char stname[20];

char  sex;

float quizz1;

float quizz2;

float assigment;

float midterm;

float final;

float total;

int numberOfitem;

};

 

student st[20];

int itemcount=0;

 

void displayheading();

void Showmax();

void Showmin();

void Sortbyid();

bool Searchduplicate(int);

//Menu contruction

 

void displaymenu(){

cout<<"============================================"<<"\n";

cout<<"                         MENU                                  "<<"\n";

cout<<"============================================"<<"\n";

cout<<"     1.Add student records"<<"\n";

cout<<"     2.Delete student records"<<"\n";

cout<<"     3.Update student records"<<"\n";

cout<<"     4.View all student records"<<"\n";

cout<<"     5.Sort student records by ID"<<"\n";

cout<<"     6.Sort student records by Total score"<<"\n";

cout<<"     7.Display average score of a selected student"<<"\n";

cout<<"     8.Display the highest and the lowest scores"<<"\n"; 

cout<<"     9.Search student by ID"<<"\n";

     }

void Add(){

  

     again:

     cout<<"Enter student's ID(1-1000):";

     cin>>st[itemcount].stnumber;

     if(Searchduplicate((int)st[itemcount].stnumber)==true){

        cout<<"This ID already exists\n";goto again;

     }

     

     cout<<"\n";

     cout<<"Enter student's Name:";   

     cin>>st[itemcount].stname;

     cout<<"\n";

     cout<<"Enter student's Sex(F or M):";cin>>st[itemcount].sex;

     cout<<"\n";

     cout<<"Enter student's quizz1 score:";cin>>st[itemcount].quizz1;

     cout<<"\n";

     cout<<"Enter student's quizz2 score:";cin>>st[itemcount].quizz2;

     cout<<"\n";

     cout<<"Enter student's assigment score:";cin>>st[itemcount].assigment;

     cout<<"\n";

     cout<<"Enter student's mid term score:";cin>>st[itemcount].midterm;

     cout<<"\n";

     cout<<"Enter student's final score:";cin>>st[itemcount].final;

     st[itemcount].total=st[itemcount].quizz1+st[itemcount].quizz2+

s[itemcount].assigment+st[itemcount].midterm+st[itemcount].final;  

    

     ++itemcount;  

    

  }

bool Searchduplicate(int id){

 bool match=false;

 for(int i=0;i<itemcount;++i){

       if(st[i].stnumber==id) match=true;}

 

   return match;

}

  

void writedata(){

//save to studentrecords.data file

     st[0].numberOfitem=itemcount;

     fstream file("studentrecords.dat",ios::out);

     file.write((char *)(&st),sizeof(st));

     file.close();

    

}

 void ViewAll(){

      int i=0;

      displayheading();

      while(i<=itemcount){

              if(st[i].stnumber!=0){

              cout<<left<<setw(5)<<st[i].stnumber<<setw(20)<<st[i].stname<<setw(5)<<st[i].sex;

              cout<<setw(5)<<st[i].quizz1<<setw(5)<<st[i].quizz2<<setw(5)<<st[i].assigment

              <<setw(5)<<st[i].midterm<<setw(5)<<st[i]. final<<setw(5)

              <<st[i].total;

             

              cout<<"\n";}

              i=i+1;

             

              }

             

                   

      }

     

void Delete(){

     int id;

     cout<<"Enter student's ID:";

     cin>>id;

     for(int i=0;i<itemcount;++i){

             if((st[i].stnumber==id)&&(itemcount!=0)){

                                                    

                            st[i].stnumber=0;

                            strcpy(st[i].stname,"0");

                            st[i].sex='0';

                            st[i].quizz1=0;

                            st[i].quizz2=0;

                            st[i].assigment=0;

                            st[i].midterm=0;

                            st[i].final=0;

                            st[i].total=0;

                           

                                    }

            

             }

     }

 

void Update(){

     int id;

     cout<<"Enter student's ID:";

     cin>>id;

     for(int i=0;i<itemcount;++i){

             if((st[i].stnumber==id)&&(itemcount!=0)){

                                                    

                            cout<<"Enter student's Name:";cin>>st[i].stname;

                            cout<<"Enter student's Sex:";cin>>st[i].sex;

                            cout<<"Enter quizz1 score:";cin>>st[i].quizz1;

                            cout<<"Enter quizz2 score:";cin>>st[i].quizz2;

                            cout<<"Enter assigment score:";cin>>st[i].assigment;

                            cout<<"Enter mid term score:";cin>>st[i].midterm;

                            cout<<"Enter final score:";cin>>st[i].final;

                            st[i].total=st[i].quizz1+st[i].quizz2+st[i].assigment+st[i].midterm+st[i].final;

                                    }

            

             }

    

     }

    

void Sortbyid(){ 


Comments

aryan shah comment

 aryan shah

hello ese koi coding h jisme 10 student ke record save ho hum cable us ke ID dale uske bare me sare information dede... plz sent me my Email ID is aryanshah5678@gmail.com plz... sent me


2014-10-24



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.