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

please help in writing this c++ program with comments. it is requested to test t

ID: 3583568 • Letter: P

Question

please help in writing this c++ program with comments. it is requested to test the program in QUINCY 2005 (free) compiler (or Dev-c++). Please make sure you get exact output for different cases as shown in photos below. The txt files (to be passed as arguments) are written at the end of this post. The program has to be compiled and executed successfully in QUINCY 2005.

text files with students records to be used

data_records_c.txt

R: 10
Steve Abrew 90 80 84 76
David Nag 93 A 90 80
Mike Black
Andrew Van Den J 88 95 85
Chris Smith 86 74 90
Dennis Dudley 74 76 77 83
Leo Rice 95 75
Fred Flinstone 73 67 78 72
Dave Light 89 71 91 89
Hua Tran Du 81 79 80

data_records_i.txt

R: 10
Steve Abrew 90 80 84 76
David Nag 93 -5 90 80
Mike Black
Andrew Van Den 117 88 95 85
Chris Smith 86 74 90
Dennis Dudley 74 76 77 83
Leo Rice 95 75
Fred Flinstone 73 67 78 72
Dave Light 89 71 91 89
Hua Tran Du 81 79 80

data_records.txt

R: 10
Steve Abrew 90 80 84 76
David Nag 93 87 90 80
Mike Black
Andrew Van Den 90 88 95 85
Chris Smith 86 74 90
Dennis Dudley 74 76 77 83
Leo Rice 95 75
Fred Flinstone 73 67 78 72
Dave Light 89 71 91 89
Hua Tran Du 81 79 80

data.txt

Steve Abrew 90,80,84,76
David Nag 93,55,90,80
Andrew Van Den 95,85

OBJECTIVE Design, debug and test a C++ program that processes a list of records stored in a text file and generates a report that shows all students' marks, average marks of students and also their highest and their lowest marks. CODING REQUIREMENTS Your program shall be well structured and designed using the concept of object based programming: follow the fundamental principles of the OOM when you define a class follow the fundamental principles of the function oriented programming to implement modules which use objects of the class defined by you Your program shall be subdivided into two files assignment3.h and assignment3.c An example of a text file with students' records to be processed is shown below. Its first line has a marker R: followed by an integer number equal to the total number of records in

Explanation / Answer

//main.cpp

#include "assignment3.h"


student* readdata(ifstream& inData, char file[], int& rec_num);
bool writedata(const student* records,ofstream& outData, int rec_num);
int find_first_mark(const string& str);
bool string_breaker(ifstream& inData, student* records, int rec_num);
int stringtoint(string& s_marks);

void student :: setStudentData(string& s_name, const int s_marks[4],int subjectcount)
{ fullname = s_name;
//cout << fullname << endl;
for (int i = 0 ; i <4 ; i++)
{
marks[i] = s_marks[i];
//cout << "mark" << i << ": " << marks[i]<< endl;
}
numofsubjects = subjectcount;
return;
}

int student :: getAverageMark() const
{
float sum = 0;
float avg = 0.0;
for (int i = 0; i< numofsubjects; i++)
{
avg +=marks[i];
}
avg = sum/numofsubjects;
avg = (int)(avg + 0.5);
return avg;
}

int student :: getLowestMark() const
{

int smallest = marks[0] ;
for ( int i=1; i < (int)sizeof(marks)/(int)sizeof(marks[0]); ++i )
if ( marks[i] < smallest )
smallest = marks[i] ;


return smallest;

}

int student :: getHighestMark() const
{

int highest = marks[0] ;
for ( int i=1; i < (int)sizeof(marks)/(int)sizeof(marks[0]); ++i )
if ( marks[i] > highest )
highest = marks[i] ;


return highest;

}

string student :: getFullName() const
{

return fullname;

}

void student :: getMarks(int mark_arr[4]) const
{
for (int i = 0; i < 4; i++)
{
mark_arr[i] = marks[i];
}

return;

}

int student :: getNumOfSubjTaken() const
{

return numofsubjects;

}

static const int argsnum = 3;

int main (int argc, char* argv[])
{
ifstream inData;
ofstream outData;
student* records;
int rec_num;

if (argc != argsnum)
{
cerr << "Invalid Number of input Parameters" << endl;
cerr << "Program Terminated" << endl;
return -1;
}


records = readdata(inData, argv[1], rec_num);
if( records == NULL)
{
inData.close();
cerr << "File Reading Unsuccesful" <<endl;
cerr << "Program Terminated" << endl;
return -1;
}

if(writedata(records, outData, rec_num) != true)
{
cerr << "File Reading Unsuccesful" <<endl;
cerr << "Program Terminated" << endl;
return -1;
}

return 0;


}

student* readdata(ifstream& inData, char file[],int& rec_num)
{
inData.open(file , ios::in);
char r_check[2];

if(!inData.good())
{
if(inData.fail()) return NULL; // invalid character
if(inData.bad()) return NULL; // hardware failure
if(inData.eof()) return NULL; // not found
}


inData >> r_check>>rec_num;
cout <<r_check<< rec_num << endl;

if(strcmp(r_check, "R:")!= 0)
{
cerr<< "No Record Count Indicated" << endl;
return NULL;
}

if(rec_num <0 || rec_num == 0)
{
cerr<< "Invalid number of Records" << endl;
return NULL;
}

student* records = new student[rec_num];


if (string_breaker(inData, records, rec_num)!= true)
{
cerr << "Unsuccesful Allocation of data to class" << endl;
return NULL;
};

if(!inData)
{
cerr << "Error Reading File" << endl;
return NULL;
}

inData.close();
return records;

}

//find position of first integer in string
int find_first_mark(const string& str)
{
size_t pos = 0;

pos = str.find_first_of("-0123456789",pos);

return pos;
}


//break string into marks and Name
bool string_breaker(ifstream& inData, student* records, int rec_num)
{

string line;
string name;
string marks;
int mark_arr[4]={0,0,0,0};
int mark[4];
int subjectcount = 0;
int i;

getline(inData,line);
//cout << "This is line" << line <<endl;

for (i = 0 ; i < rec_num; i++)
{

getline(inData,line);
size_t pos = find_first_mark(line);
if (pos == string::npos)
{
name = line;

subjectcount = -1;
//cout << "Name :" << name << marks << endl;
}
else
{
name = line.substr(0,pos);
marks = line.substr(pos);
//cout << marks << endl;
}

size_t pos_start = 0 ;
size_t pos_end =0;
string temp;

if (subjectcount!=-1){
for (int j = 0; j<4 ; j++)
{

pos_end = marks.find(" ", pos_start);
if (pos_end == string::npos)
{
pos_end = (marks.length() -1);
subjectcount--;
}

//cout <<"POS_start is "<< pos_start<< "POS_end is " << pos_end << endl;

if (pos_end == string::npos)
{
cerr << "Marks Contain Characters" <<endl;
return false;
}
temp = marks.substr(pos_start,(pos_end-pos_start));


mark_arr[j] = stringtoint(temp);
if (mark_arr[j] == -1) return false;
else
subjectcount++;


//cout << mark_arr[j] << endl;
pos_start = pos_end+1;
}
}
//cout << "Name :" << name << marks<<endl<< mark_arr [0] <<" " <<mark_arr[1]<<" " <<mark_arr[2]<<" " <<mark_arr[3]<< endl;
subjectcount++;
cout << subjectcount << endl;
records[i].setStudentData(name,mark_arr,subjectcount);

subjectcount = 0;
}
for(i = 0 ; i< rec_num ; i++){
records[i].getMarks(mark);
cout << records[i].getFullName() << endl << mark[0]<< endl << " " << mark[1]<< endl << " " <<mark[2]<< endl << " " << mark[3] << endl<<"subj: " << records[i].getNumOfSubjTaken()<< endl;
}
return true;

}

int stringtoint(string& s_mark)
{
int mark ;
int temp_i;

for (int i = 0; i< (int)s_mark.length();i++)
{
temp_i = (s_mark.c_str())[i];
//cout << "int " << temp_i<<" length: " << (int)s_mark.length() << endl;
if((temp_i >= 48) && (temp_i <= 57))
{
//cout << "Digit is a number" << endl;
}

else
{
cerr << "Characters input as marks" <<endl;
return -1;
}
}

mark= atoi(s_mark.c_str());


return mark;
}

bool writedata(const student* records,ofstream& outData, int rec_num)
{
int avg;
int lowest_mark;
int highest_mark;
int marks[4];

outData.open("report.txt", ios::out | ios::trunc);
if (!outData)
{
cerr << "Error Opening Writing File" << endl;
return false;
}

outData << "Full Name Mark1 Mark2 Mark3 Average Min Max " << endl;
for (int i = 0; i < rec_num ; i++)
{
records[i].getMarks(marks);
outData << records[i].getFullName() << " " << marks[0] << " " << marks[1] << " " <<marks[2] << " " << marks[3] << endl;
cout << records[i].getFullName() << " " << marks[0] << " " << marks[1] << " " <<marks[2] << " " << marks[3] << endl;
}
if(!outData)
{
cerr << "Error Writing File" << endl;
outData.close();
return false;
}

outData.close();
return true;
}

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

//assignment3.h

#ifndef _asm3_
#define _asm3_

#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;


class student
{
private:
string fullname;
int marks[4];
int numofsubjects;

public:
void setStudentData(string& s_name, const int s_marks[4],int subjectcount);
string getFullName()const;
void getMarks(int mark_arr [4]) const;
int getNumOfSubjTaken() const;
int getAverageMark() const;
int getLowestMark() const;
int getHighestMark() const;
};

#endif

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