Ethan O.

asked • 02/22/20

How do I write this program into a text file

#include <iostream>

#include <fstream>

#include <string>

#include <cstring>

#include <cstdlib>

#include <iomanip>


using namespace std;


// global declarations for constants

const int MAX_ANSWERS = 20;

const int MAX_STUDENTS = 200;

const int MAX_SCORE = 40;


// function prototypes

void readDataFromFile(string fileName, string *correctAnswers, string *studIDs, string *studAnswers);

void computeStudScores(string correctAnswers, string* studAnswers, double *studscores);

void computeStudGrades(double *studscores, char *studgrades);

void displayOutput(string *studIDS, string *studAnswers, double *studscores, char *studgrades);


// global declarations for a variable

int studentsCounter = 0;


// start main function

int main()

{

// variables declaration

fstream outfile;

string fileName;

string correctAnswers;

string *studIDS = new string[MAX_STUDENTS];

string *studAnswers = new string[MAX_STUDENTS];

double *studScores = new double[MAX_STUDENTS];

char *studGrades = new char[MAX_STUDENTS];

outfile.write("studentAnswers.txt", ios::out | ios:: app);



// invoke the function readDataFromFile()

readDataFromFile(fileName, &correctAnswers, studIDS, studAnswers);


// invoke the function compute_Stud_Scores()

computeStudScores(correctAnswers, studAnswers, studScores);


// invoke the function compute_Stud_Grades

computeStudGrades(studScores, studGrades);


// invoke the function displayOutput

displayOutput(studIDS, studAnswers, studScores, studGrades);


outfile.close();


return 0;

}


// readDataFromFile() function accepts a string, string reference, string array, and string array

void readDataFromFile(string fileName, string *correctAnswers, string *studIDs, string *studAnswers)

{

// declare the required variables

ifstream inFile;

string line;

int position;

int index;


// open the file

inFile.open("student_test.txt");


// condition to check if the inFile does not exists

if (!inFile)

{

//display an error message and exit from the program

cout << "Unable to open the file \"" << fileName << "\""

<< endl;

} // end if


// read the correct answers from the file

getline(inFile, *correctAnswers);


// set the index to 0

studentsCounter = 0;


// get the next line

getline(inFile, line);


index = 0;


// loop until there are no more lines in the file

while (inFile)

{

// find the position of " " in the line

position = line.find_first_of(" ");


// substring the line and get the student ID

studIDs[index] = line.substr(0, position);


// substring the line and get the student answers

studAnswers[index] = line.substr(position + 1);


// increment the index

index++;


// get the next line

getline(inFile, line);

}


// close the file

inFile.close();



studentsCounter = index;

}


// compute_Stud_Scores function that accepts string variable, string array, and double array

void computeStudScores(string correctAnswers, string* studAnswers, double *studscores)

{

// declare the required variables

string student_Ans;

double scoreStud;


// loop through each student

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

{

// retrieve the student answers present at index i

student_Ans = studAnswers[i];


// set the score of the student to 0 initially

scoreStud = 0;


// loop through each answer

for (int j = 0; j < student_Ans.length(); j++)

{

// check whether the character at j of student_Ans is blank

if (student_Ans.at(j) == ' ')

{

// add 0 score to the current answer

scoreStud = scoreStud + 0;

}

// otherwise, check if student_Ans is equal to the

// correctAnswers

else if (student_Ans.at(j) == correctAnswers.at(j))

{

// if correct, add +2 to the existing score

scoreStud = scoreStud + 2;

}

// otherwise, check if student_Ans is not equal to the

// correctAnswers

else

{

// if wrong, add -1 to the score

scoreStud = scoreStud - 1;

}

}

// now set the score of the student in stud_scores array

studscores[i] = scoreStud;

}

}


// compute_Stud_Grades() function that accetps a string, array of strings and double

// array

void computeStudGrades(double *studscores, char *studGrades)

{

// declare the required variable

double studpercent;


// loop through each student scores

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

{

// compute the average score of the student in terms of

// percentages

studpercent = (studscores[i] / MAX_SCORE) * 100;


// depending on the stud_percent, set the grade of the

// student

// if the stud_percent is within the range of 90 - 100

// set the grade to A

if ((studpercent >= 90) && (studpercent <= 100))

{

studGrades[i] = 'A';

}


// if the stud_percent is within the range of 80 - 89.99

// set the grade to B

else if ((studpercent >= 80) && (studpercent <= 89.99))

{

studGrades[i] = 'B';

}


// if the stud_percent is within the range of 70 - 79.99

// set the grade to C

else if ((studpercent >= 70) && (studpercent <= 79.99))

{

studGrades[i] = 'C';

}


// if the stud_percent is within the range of 60 - 69.99

// set the grade to D

else if ((studpercent >= 60) && (studpercent <= 69.99))

{

studGrades[i] = 'D';

}


// if the stud_percent is within the range of 0 - 59.99

// set the grade to F

else

{

studGrades[i] = 'F';

}

}

}


// displayOutput() function accepts two string arrays, one double array, and a character array.

void displayOutput(string *studIDS, string *studAnswers, double *studscores, char *studgrades)

{

// display the header

cout << "Students Results:" << endl;

cout << "Student ID\tStudent Answers\t\tScore\tGrade" << endl;

cout << "-----------------------------------------------------" << endl;


cout << setprecision(2) << fixed;


// loop through each student

// display the result

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

{

cout<< studIDS[i] << "\t" << studAnswers[i] << "\t" << studscores[i] << "\t" << studgrades[i] << endl;

}

}


1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.