Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++: This assignment explores reading and writing binary files 1. Create two

ID: 3839474 • Letter: I

Question

In C++:

This assignment explores reading and writing binary files

1. Create two simple ascii text files. One text file will store first names, the other file will store last names. There should be a minimum of 20 names in each file. The names should be randomly created and stored in the files. The files should have the same number of names. Do not sort the names in the files. The length of the names should be 15 characters or less and may contain spaces. These names will be stored in character arrays in

2. Create a class named Student The class should have 4 fields: first name, last name, student number and GPA The fields for the first and last names should be character arrays of size 16 You may create multiple overloaded constructors but you must create a constructor that takes 4 parameters: student number, first name, last name, and GPA Create appropriate set and get functions Create a function named getStudentRecord that will return a formatted string containing the state of a Student object. The string returned by the function should not contain any newline characters (so that the record can be displayed on a single line).

3. You will need to create functions (non-member functions) that generate random numbers (C++11 standard random number generation) for a GPA and a student number. GPA's should be in the range of 1.00 - 4.00 (data type double). Student numbers should be in the range of 1000 - 4000 (data type int) with no duplicates!

4. Create an empty vector that you will use to store Student objects.

5. In a loop: Create a minimum of 20 Student objects by reading a first name and a last name from your text files and using your functions to create a student number and a GPA. Store the Student object in your vector.

6. After you have loaded the vector with Student objects write the objects to a BINARY file.

7. Open the binary file of Student objects and in a loop: read a Student object display it to the screen using an output statement. Each student record should be display on a single line

Explanation / Answer

#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
using namespace std;
class Student
{  
   int number;
   string f_name;
   string l_name;
   double gpa;
  
   public :
       Student(int num, string f_n,   string l_n, double g ){
           number = num;
           f_name = f_n;
           l_name = l_n;
           gpa = g;
       }
      
       int getNumber(){
           return number;
       }
       string gatFistName(){
           return f_name;
       }
       string gatLastName(){
           return l_name;
       }
       double getGPA(){
           return gpa;
       }
       void display(){
           cout <<"Number: "<<number<<endl;
           cout <<"Fist Name: "<<f_name<<endl;
           cout <<"Last Name : "<<l_name<<endl;
           cout <<"GPA : "<<gpa<<endl;
       }
};


int getRand(int limit){
   int v = rand() % limit + 1;
   return v;
}

int main(int argc,char* argv[]){

   ifstream fistNameFile;
fistNameFile.open("first_name.txt",ios::in); // opning file for fist name
   ifstream lastNameFile;
lastNameFile.open("larst_name.txt",ios::in); // opning file for last name

ofstream outfile;
outfile.open("student.txt",ios::out); // opning file to write all students details
   std::vector<Student*> list;
   string line;
   string line1;
   cout << "Displaying records " << endl;
   int num=1;
while (num < 20)
   {  
       num++;
       fistNameFile >> line; // reading fist name file
       lastNameFile >> line1; // reading last name from file
       int num = getRand(1000); // generating student number randomly
       double gpa = (double)getRand(4); // generating gpa randomly
       Student * stu = new Student(num,line,line1,gpa); // creating student object
       stu->display(); // displaying on screen
       list.push_back(stu); // adding in vector
       outfile <<num<<" "<< line <<" "<<line1<<" "<<gpa<<endl; // writing in output file
       cout << line << endl;
      
   }
   cout << "output is saved in student.txt file " << endl;
fistNameFile.close();
   lastNameFile.close();
outfile.close();
return 0;
}