I need help with binary files. I\'m using Microsoft Studio 2017. This is the que
ID: 3851257 • Letter: I
Question
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 the previous question:
This is my previous code
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
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());
}
Executive Summary: In this project, you are asked to design, develop, implement, and test a prototype (a model) for an exam software system. This exam is administered to students graduating with a degree in Social Science who would like to enter the Graduate Program. The students' must pass this exam to be admitted into the Graduate Program. The basic requirements for such a system include the following: l. Student Registration: Students taking the exam are entered into the grading system via an input file, a text file. The file contains the students' names, ID numbers, and answers to a multiple choice exam. 2. Grading: The user enters an "Answer Key" and then the system grades the students' answers and calculates the students' total points, average, and letter grade earned 3. Grade Reporting: The system generates a report (in ascending order) of each student's name, ID number, exam answers, total points, average and letter grade earned 4. Analysis Reporting: The system produces reports (in ascending order) that include the names and graded results of the students who made an A, B, or C full admission into the program), the names and graded results of the students who made a D (conditional admission into the program), and the names and graded results of the students who failed not admitted to the program 5. Searching: The system is able to find and report a specific student's results based on the student's ID entered by the user 6. (Bonus) Sort: You may sort records by name You may use following function prototypes and structures: //Reads the student name, ID, and Answers from an input file void get Information (Student Record student int& num std); //Allows user to enter the Answer Key void enter Key (char key //calculates average grade for each student void calculateAveAndLetter (Student Record. student int number of students char Displays all information for each student void sortRecordsByName (Student Record student int number of students) //optional void displayResults (StudentRecord student int number of students) bool display (StudentRecord student int number of students) Report void search ID (Student Record student int number of students) struct Answers char answerl char answer2; char answer3Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
struct StudentData
{
int studentID;
string first_name;
string last_name;
int exam1;
int exam2;
int exam3;
int total;
char ch;
};
const int SIZE = 9;
const int INFO = 4;
void openInputFile(ifstream &, string);
void getTotal(StudentData[]);
void getGrade(StudentData[]);
void calcLowest(StudentData[], int &, int &, int &, int &, int[]);
void calcHighest(StudentData[], int &, int &, int &, int &, int[]);
void getAverage(StudentData[], int, double &, double &, double &, double &, double[]);
void getStd(StudentData[], double &, double &, double &, double &, double &, double &, double &, double &, double[]);
void print(StudentData[], int[], int[], double[], double[]);
void sort(StudentData[]);
int main()
{
StudentData arr[SIZE];
int lowest1, lowest2, lowest3, lowest4;
int highest1, highest2, highest3, highest4;
double average1 = 0, average2 = 0, average3 = 0, average4 = 0;
double std1 = 0, std2 = 0, std3 = 0, std4 = 0;
int lowest[INFO] = {};
int highest[INFO] = {};
double average[INFO] = {};
double standardDeviation[INFO] = {};
ifstream inFile;
string inFileName = "C:/********************";
openInputFile(inFile, inFileName);
for(int count = 0; count < SIZE; count++)
{
inFile >> arr[count].studentID >> arr[count].first_name >> arr[count].last_name >> arr[count].exam1 >> arr[count].exam2 >> arr[count].exam3;
}
inFile.close();
getTotal(arr);
getGrade(arr);
calcLowest(arr, lowest1, lowest2, lowest3, lowest4, lowest);
calcHighest(arr, highest1, highest2, highest3, highest4, highest);
getAverage(arr, SIZE, average1, average2, average3, average4, average);
getStd(arr, std1, std2, std3, std4, average1, average2, average3, average4, standardDeviation);
cout << " ";
print(arr, lowest, highest, average, standardDeviation);
cout << " ";
sort(arr);
print(arr, lowest, highest, average, standardDeviation);
system("PAUSE");
return 0;
}
void openInputFile(ifstream &inFile, string inFileName)
{
inFile.open(inFileName);
if (!inFile)
{
cout << "Error to open file." << endl;
cout << endl;
return;
}
}
void getTotal(StudentData arr[])
{
for(int i = 0; i < SIZE; i++)
{
arr[i].total = arr[i].exam1 + arr[i].exam2 + arr[i].exam3;
}
}
void getGrade(StudentData arr[])
{
for(int i = 0; i < SIZE; i++)
{
if(arr[i].total >= 270)
arr[i].ch = 'A';
else if(arr[i].total >= 240)
arr[i].ch = 'B';
else if(arr[i].total >= 210)
arr[i].ch = 'C';
else if(arr[i].total >= 180)
arr[i].ch = 'D';
else
arr[i].ch = 'F';
}
}
void calcLowest(StudentData arr[], int &lowest1, int &lowest2, int &lowest3, int &lowest4, int lowest[])
{
int smallest = 0;
lowest1 = arr[0].exam1;
lowest2 = arr[0].exam2;
lowest3 = arr[0].exam3;
lowest4 = arr[0].total;
for (int i = 0; i < SIZE; i++)
{
if (lowest1 > arr[i].exam1)
{
lowest1 = arr[i].exam1;
smallest = i;
}
if (lowest2 > arr[i].exam2)
{
lowest2 = arr[i].exam2;
smallest = i;
}
if (lowest3 > arr[i].exam3)
{
lowest3 = arr[i].exam3;
smallest = i;
}
if (lowest4 > arr[i].total)
{
lowest4 = arr[i].total;
smallest = i;
}
}
for(int index = 0; index < INFO; index++)
{
if(index == 0)
lowest[0] = lowest1;
else if(index == 1)
lowest[1] = lowest2;
else if(index == 2)
lowest[2] = lowest3;
else if(index == 3)
lowest[3] = lowest4;
else
cout << "Fail!" << endl;
}
}
void calcHighest(StudentData arr[], int &highest1, int &highest2, int &highest3, int &highest4, int highest[])
{
int biggest = 0;
highest1 = arr[0].exam1;
highest2 = arr[0].exam2;
highest3 = arr[0].exam3;
highest4 = arr[0].total;
for (int i = 0; i < SIZE; i++)
{
if (highest1 < arr[i].exam1)
{
highest1 = arr[i].exam1;
biggest = i;
}
if (highest2 < arr[i].exam2)
{
highest2 = arr[i].exam2;
biggest = i;
}
if (highest3 < arr[i].exam3)
{
highest3 = arr[i].exam3;
biggest = i;
}
if (highest4 < arr[i].total)
{
highest4 = arr[i].total;
biggest = i;
}
}
// Loop highest values into an array of size 4
for(int index = 0; index < INFO; index++)
{
if(index == 0)
highest[0] = highest1;
else if(index == 1)
highest[1] = highest2;
else if(index == 2)
highest[2] = highest3;
else if(index == 3)
highest[3] = highest4;
else
cout << "Fail!" << endl;
}
}
void getAverage(StudentData arr[], int size, double &average1, double &average2, double &average3, double &average4, double average[])
{
int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
for(int i = 0; i < SIZE; i++)
{
sum1 += arr[i].exam1;
sum2 += arr[i].exam2;
sum3 += arr[i].exam3;
sum4 += arr[i].total;
}
// Calculate average for each category
average1 += static_cast<double>(sum1)/size;
average2 += static_cast<double>(sum2)/size;
average3 += static_cast<double>(sum3)/size;
average4 += static_cast<double>(sum4)/size;
for(int index = 0; index < INFO; index++)
{
if(index == 0)
average[0] = average1;
else if(index == 1)
average[1] = average2;
else if(index == 2)
average[2] = average3;
else if(index == 3)
average[3] = average4;
else
cout << "Fail!" << endl;
}
}
void getStd(StudentData arr[], double &std1, double &std2, double &std3, double &std4, double &average1, double &average2, double &average3, double &average4, double standardDeviation[])
{
double deviationSum1 = 0, deviationSum2 = 0, deviationSum3 = 0, deviationSum4 = 0;
for(int i = 0; i < SIZE; i++)
{
deviationSum1 += pow((arr[i].exam1 - average1), 2);
deviationSum2 += pow((arr[i].exam2 - average2), 2);
deviationSum3 += pow((arr[i].exam3 - average3), 2);
deviationSum4 += pow((arr[i].total - average4), 2);
}
std1 = sqrt(deviationSum1 / ((SIZE) - 1));
std2 = sqrt(deviationSum2 / ((SIZE) - 1));
std3 = sqrt(deviationSum3 / ((SIZE) - 1));
std4 = sqrt(deviationSum4 / ((SIZE) - 1));
// Loop average values into an array of size
for(int index = 0; index < INFO; index++)
{
if(index == 0)
standardDeviation[0] = std1;
else if(index == 1)
standardDeviation[1] = std2;
else if(index == 2)
standardDeviation[2] = std3;
else if(index == 3)
standardDeviation[3] = std4;
else
cout << "Fail!" << endl;
}
}
cout << " ";
}
void sort(StudentData arr[])
{
StudentData temp;
for (int i = 0; i < (SIZE - 1); i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arr[i].last_name > arr[j].last_name)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.