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

Ive had to post this question 4 times, can I please get actual help with this. I

ID: 3851340 • Letter: I

Question

Ive had to post this question 4 times, can I please get actual help with this.

I need help with binary files. I'm using Microsoft Studio 2017.

This is the question I need help with: Only change is: you will first write student structures that were given in Homework Assignment 2 into a binary file and read those student structures from a binary file into your student's array. Just make necessary changes in your code in Assignment 2 to work with binary files.

Here is the student.txt file.

This is my previous code

#include

#include

#include

#include

#include

using namespace std;

void readDataFromFile();

void calculateResult();

void displayResult();

void removeCharsFromString(string &str);

char file_name[20] = "students.txt";

int total_record = 0;

char answer_keys[5];

struct Student

{

   int stud_id;

   string full_name, answers;

   char lLtterGde;

   double total_points, avg;

   int operator=(const Student& s)

   {      

       this->stud_id = s.stud_id;

       this->full_name = s.full_name;

       this->lLtterGde = s.lLtterGde;

       this->answers = s.answers;

       this->total_points = s.total_points;

       this->avg = s.avg;

       return 0;

   }

}record[50];

void StudentSort()

{

   int li, lj;

   Student check;

   for (li = 0; li < total_record; li++) {

       check = record[li];

       for (lj = li; lj >= 1 && (check.full_name < record[lj - 1].full_name); lj--)

       {

           record[lj] = record[lj - 1];

           record[lj - 1] = check;

       }

   }

}

int main()

{

   cout << endl << "Enter Answer Keys : ";

   for (int li = 0; li<5; li++)

   {

       cout << endl << "Answer " << li + 1 << " : ? ";

       cin >> answer_keys[li];

   }

   calculateResult();

   displayResult();

   system("pause");

   return 0;

}

void readDataFromFile()

{

   ifstream ifile;

   ifile.open(file_name);

   total_record = 0;

   string line = "";

   if (ifile)

   {

       while (!ifile.eof())

       {

           getline(ifile, line);

           string id = line.substr(0, line.find(" "));

           record[total_record].stud_id = atoi(id.c_str());

           record[total_record].full_name = line.substr(line.find(" ") + 1);

           line = "";

           getline(ifile, line);

           record[total_record].answers = line;

           total_record++;

       }

       if (total_record > 0)

           total_record--;

   }

   else

       cout << "Error";

   ifile.close();

   StudentSort();

}

void calculateResult()

{

   cout << endl << "Grade Report" << endl;

   cout << endl << "Student ID Student Name Answers Total Pts Average Letter Grade ";

   readDataFromFile();

   for (int li = 0; li < total_record; li++)

   {

       removeCharsFromString(record[li].answers);

       int points = 0;

       for (int lj = 0; lj<10; lj++)

       {

           if (record[li].answers[lj] == answer_keys[lj])

           {

           points += 5;

           }

       }

       record[li].total_points = points;

       record[li].avg = (points * 100 / 50);

       if (record[li].avg >= 90)

       {

       record[li].lLtterGde = 'A';

       }

       else if (record[li].avg >= 80 && record[li].avg <= 89)

       {

           record[li].lLtterGde = 'B';

       }

       else if (record[li].avg >= 70 && record[li].avg <= 79)

       {

           record[li].lLtterGde = 'C';

       }

       else if (record[li].avg >= 60 && record[li].avg <= 69)

       {

           record[li].lLtterGde = 'D';

       }

       else

       {

           record[li].lLtterGde = 'F';

       }

       cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(12) << record[li].answers << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

}

void displayResult()

{

   cout << endl << "Student Admitted to the Graduate Program" << endl;

   cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

   for (int li = 0; li < total_record; li++)

   {

       if (record[li].lLtterGde == 'A' || record[li].lLtterGde == 'B' || record[li].lLtterGde == 'C')

           cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

   cout << endl << "Student with Conditional Admission to the Graduate Program" << endl;

   cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

   for (int li = 0; li < total_record; li++)

   {

       if (record[li].lLtterGde == 'D')

           cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

   cout << endl << "Student not Allowed Admission" << endl;

   cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

   for (int li = 0; li < total_record; li++)

   {

       if (record[li].lLtterGde == 'F')

       cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

   }

}

void removeCharsFromString(string &str)

{

   //str.erase(remove(str.begin(), str.end(), ' '), str.end());

}

Explanation / Answer

Below is the code for your question. The basic flow of the program is

1. load records from text file students.txt

2. write these records to a binary file students.bin

3. clear all records

4. now read the records from students.bin binary file

5. calculate the results and display

The step to clear records has been done just to show that the records were indeed loaded again from binary file and not the ones that were loaded from text file.

Note: please make sure your input file is following the desired format strictly. Otherwise the program will not function correctly.

Format should be

<number of records>

<student_id> <space> <full name>

<answer1> <answer2> <answer3> <answer4> <answer5>

<total_points> <avg> <grade>

The last 3 lines will repeat as many times as number of students.

In case you have any issues running the code, post a comment and I shall respond. If happy with the answer , request you to rate the answer. Thank you very much.

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

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <iomanip>

//#include

using namespace std;

void readDataFromTextFile();

void calculateResult();

void displayResult();

void writeToBinaryFile(string fname);

void readFromBinaryFile(string fname);

char file_name[100] = "students.txt";

int total_record = 0;

char answer_keys[5];

struct Student

{

int stud_id;

string full_name;

char answers[5];

char lLtterGde;

double total_points, avg;

int operator=(const Student& s)

{

this->stud_id = s.stud_id;

this->full_name = s.full_name;

this->lLtterGde = s.lLtterGde;

  

for(int i = 0; i < 5 ;i++)

this->answers[i] = s.answers[i];

  

this->total_points = s.total_points;

this->avg = s.avg;

return 0;

}

}record[50];

void StudentSort()

{

int li, lj;

Student check;

for (li = 0; li < total_record; li++) {

check = record[li];

for (lj = li; lj >= 1 && (check.full_name < record[lj - 1].full_name); lj--)

{

record[lj] = record[lj - 1];

record[lj - 1] = check;

}

}

}

void clearRecords()

{

cout << " clearing all records..." << endl;

for(int i = 0; i < total_record; i++)

{

record[i].full_name = "";

record[i].stud_id = 0;

record[i].total_points = 0;

record[i].avg = 0;

for(int j = 0; j < 5; j++)

record[i].answers[j] = ' ';

record[i].lLtterGde = ' ';

}

}

int main()

{

cout << endl << "Enter Answer Keys : ";

for (int li = 0; li<5; li++)

{

cout << endl << "Answer " << li + 1 << " : ? ";

cin >> answer_keys[li];

}

  

readDataFromTextFile();

writeToBinaryFile("student.bin"); //write the structs loaded from text file into a binary file

total_record = 0; //reset to 0 and read from binary file

clearRecords();

readFromBinaryFile("student.bin");

calculateResult();

displayResult();

system("pause");

return 0;

}

void readDataFromTextFile()

{

cout << " reading records from text file...." << endl;

ifstream ifile;

  

ifile.open(file_name);

ifile >> total_record ;

string line = "";

if (ifile)

{

for(int i = 0; i < total_record; i++)

{

getline(ifile, line);//newline

getline(ifile, line);

  

string id = line.substr(0, line.find(" "));

record[i].stud_id = atoi(id.c_str());

record[i].full_name = line.substr(line.find(" ") + 1);

  

for(int j = 0; j < 5; j++)

ifile >> record[i].answers[j];

  

ifile >> record[i].total_points >> record[i].avg >> record[i].lLtterGde;

  

}

}

else

cout << "Error";

ifile.close();

StudentSort();

}

void calculateResult()

{

cout << endl << "Grade Report" << endl;

cout << endl << "Student ID Student Name Answers Total Pts Average Letter Grade ";

  

for (int li = 0; li < total_record; li++)

{

  

int points = 0;

for (int lj = 0; lj<5; lj++)

{

if (record[li].answers[lj] == answer_keys[lj])

{

points += 10;

}

}

record[li].total_points = points;

record[li].avg = (points * 100 / 50);

if (record[li].avg >= 90)

{

record[li].lLtterGde = 'A';

}

else if (record[li].avg >= 80 && record[li].avg <= 89)

{

record[li].lLtterGde = 'B';

}

else if (record[li].avg >= 70 && record[li].avg <= 79)

{

record[li].lLtterGde = 'C';

}

else if (record[li].avg >= 60 && record[li].avg <= 69)

{

record[li].lLtterGde = 'D';

}

else

{

record[li].lLtterGde = 'F';

}

cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(12) << record[li].answers << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

}

}

void displayResult()

{

cout << endl << "Student Admitted to the Graduate Program" << endl;

cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

for (int li = 0; li < total_record; li++)

{

if (record[li].lLtterGde == 'A' || record[li].lLtterGde == 'B' || record[li].lLtterGde == 'C')

cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

}

cout << endl << "Student with Conditional Admission to the Graduate Program" << endl;

cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

for (int li = 0; li < total_record; li++)

{

if (record[li].lLtterGde == 'D')

cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

}

cout << endl << "Student not Allowed Admission" << endl;

cout << endl << "Student ID Student Name Total Pts Average Letter Grade ";

for (int li = 0; li < total_record; li++)

{

if (record[li].lLtterGde == 'F')

cout << endl << setw(4) << record[li].stud_id << setw(22) << record[li].full_name << setw(8) << record[li].total_points << setw(8) << record[li].avg << setw(8) << record[li].lLtterGde;

}

}

void writeToBinaryFile(string fname)

{

cout << " writing to binary file " << fname << " ... " << endl;

ofstream binfile(fname.c_str(), ios::out | ios::binary);

binfile.write((char*)&total_record, sizeof(int)); //first write the number of records

  

for(int i = 0; i < total_record; i++) //now write each record

{

binfile.write((char*)&record[i], sizeof(Student));

}

binfile.close();

}

void readFromBinaryFile(string fname)

{

cout << " reading records from binary file " << fname << " ... " << endl;

ifstream binfile(fname.c_str(), ios::in | ios::binary);

binfile.read((char*)&total_record, sizeof(int)); //first read number of records

for(int i = 0; i < total_record; i++) //now load each record

{

binfile.read((char*)&record[i], sizeof(Student));

}

binfile.close();

}

test input file : students.txt

10

878 Adams, Jerry

A C D C A

50.0 100.0 A

44 Ball, Lee

A B D C A

40.0 80.0 B

666 Dunn, Bob

A B D C A

40.0 80.0 B

88 Hall, Bill

A C D A B

30.0 60.0 D

55 Hill, Nathan

A C B A B

20.0 40.0 F

111 Jones, Paul

A C D C A

50.0 100.0 A

333 Land, Chris

C D B C A

20.0 40.0 F

998 Miles, Sue

A C D B B

30.0 60.0 D

66 Rowe, Steve

A C D C A

50.0 100.0 A

22 Smith, Mary

A C D B A

40.0 80.0 B

Enter Answer Keys :
Answer 1 : ? A

Answer 2 : ? C

Answer 3 : ? D

Answer 4 : ? C

Answer 5 : ? A

reading records from text file....

writing to binary file student.bin ...

clearing all records...

reading records from binary file student.bin ...

Grade Report

Student ID Student Name Answers Total Pts Average Letter Grade
878 Adams, Jerry ACDCAA 50 100 A
44 Ball, Lee ABDCAB 40 80 B
666 Dunn, Bob ABDCAB 40 80 B
88 Hall, Bill ACDABD 30 60 D
55 Hill, Nathan ACBABF 20 40 F
111 Jones, Paul ACDCAA 50 100 A
333 Land, Chris CDBCAF 20 40 F
998 Miles, Sue ACDBBD 30 60 D
66 Rowe, Steve ACDCAA 50 100 A
22 Smith, Mary ACDBAB 40 80 B
Student Admitted to the Graduate Program

Student ID Student Name Total Pts Average Letter Grade
878 Adams, Jerry 50 100 A
44 Ball, Lee 40 80 B
666 Dunn, Bob 40 80 B
111 Jones, Paul 50 100 A
66 Rowe, Steve 50 100 A
22 Smith, Mary 40 80 B
Student with Conditional Admission to the Graduate Program

Student ID Student Name Total Pts Average Letter Grade
88 Hall, Bill 30 60 D
998 Miles, Sue 30 60 D
Student not Allowed Admission

Student ID Student Name Total Pts Average Letter Grade
55 Hill, Nathan 20 40 F
sh: pause: command not found
333 Land, Chris 20 40 F