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

C++ Programming Hello! I have this code. It works however, it needs to ouput the

ID: 3724232 • Letter: C

Question

C++ Programming

Hello! I have this code. It works however, it needs to ouput the results onto a .txt file called "Out.txt". Can somebody help add this into the code? Any help is greatly appreciated!!

RESTRICTIONS:

No global variables

No labels or go-to statements

No infinite loops

No break statements

CODE:

#include <iostream>

#include <string>

#include <fstream>

#include <iomanip>

using namespace std;

struct studentType

{

string studentFName;

string studentLName;

int testScore;

char grade;

};

void getData(ifstream& inFile, studentType sList[], int listSize);

void calculateGrade(studentType sList[], int listSize);

int highestScore(const studentType sList[], int listSize);

void printResult(const studentType sList[], int listSize);

int main()

{

ifstream in;

in.open("Data.txt");

if (in.fail()) //is it ok?

{

cout << "file did not open please check it ";

system("pause");

system("exit");

}

studentType sList[20];

getData(in, sList, 20);

calculateGrade(sList, 20);

printResult(sList, 20);

in.close();

system("pause");

return 0;

}

void getData(ifstream& inFile, studentType sList[], int listSize)

{

int n = 0;

while (n<listSize)

{

inFile >> sList[n].studentLName >> sList[n].studentFName >> sList[n].testScore;

n++;

}

}

void calculateGrade(studentType sList[], int listSize)

{

int i;

for (i = 0; i<listSize; i++)

if (sList[i].testScore<60)

sList[i].grade = 'F';

else if (sList[i].testScore<70)

sList[i].grade = 'D';

else if (sList[i].testScore<80)

sList[i].grade = 'C';

else if (sList[i].testScore<90)

sList[i].grade = 'B';

else

sList[i].grade = 'A';

}

int highestScore(const studentType sList[], int listSize)

{

int high = 0, i;

for (i = 0; i<listSize; i++)

if (high < sList[i].testScore)

high = sList[i].testScore;

return high;

}

void printResult(const studentType sList[], int listSize)

{

cout << left << setw(30) << "Student Name" << right << setw(10) << "TestScore" << right << setw(7) << "Grade" << endl;

string name;

int high, i;

for (i = 0; i<listSize; i++)

{

name = sList[i].studentLName + ", " + sList[i].studentFName;

cout << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;

}

cout << endl;

high = highestScore(sList, listSize);

cout << "Highest Test Score: " << high << endl;

cout << "Students having the highest test score: " << endl;

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

if (sList[i].testScore == high)

cout << sList[i].studentLName << ", " << sList[i].studentFName << endl;

}

Explanation / Answer

printResult() function is modified to write into a file. output file "out.txt" iss opened and closed in mani().

Modified Code:

#include <iostream>

#include <string>

#include <fstream>

#include <iomanip>

using namespace std;

struct studentType

{

                string studentFName;

                string studentLName;

                int testScore;

                char grade;

};

void getData(ifstream& inFile, studentType sList[], int listSize);

void calculateGrade(studentType sList[], int listSize);

int highestScore(const studentType sList[], int listSize);

void printResult(ofstream& out, const studentType sList[], int listSize);

int main()

{

                ifstream in;

                in.open("Data.txt");

                if (!in)

                {

                                cout << "file did not open please check it ";

                                system("pause");

                                system("exit");

                }

               

                ofstream out("out.txt"); //output file     

               

                studentType sList[20];  

                getData(in, sList, 20);     

                calculateGrade(sList, 20);             

                printResult(out, sList, 20);

               

                in.close();

                out.close();

               

                system("pause");

               

                return 0;

}

void getData(ifstream& inFile, studentType sList[], int listSize)

{

                int n = 0;

               

                while (n<listSize)             

                {             

                                inFile >> sList[n].studentLName >> sList[n].studentFName >> sList[n].testScore;

                                n++;

                }

}

void calculateGrade(studentType sList[], int listSize)

{

                int i;

               

                for (i = 0; i<listSize; i++)

                                if (sList[i].testScore<60)

                                                sList[i].grade = 'F';                           

                                else if (sList[i].testScore<70)                      

                                                sList[i].grade = 'D';                           

                                else if (sList[i].testScore<80)                      

                                                sList[i].grade = 'C';                           

                                else if (sList[i].testScore<90)                      

                                                sList[i].grade = 'B';                           

                                else

                                                sList[i].grade = 'A';

}

int highestScore(const studentType sList[], int listSize)

{

                int high = 0, i;

                for (i = 0; i<listSize; i++)

                                if (high < sList[i].testScore)

                                                high = sList[i].testScore;

               

                return high;

}

void printResult(ofstream& out, const studentType sList[], int listSize)

{

                out << left << setw(30) << "Student Name" << right << setw(10) << "TestScore" << right << setw(7) << "Grade" << endl;

               

                string name;

                int high, i;

               

                for (i = 0; i<listSize; i++)

                {

                                name = sList[i].studentLName + ", " + sList[i].studentFName;

                                out << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;

                }

                out << endl;

               

                high = highestScore(sList, listSize);

                out << "Highest Test Score: " << high << endl;

                out << "Students having the highest test score: " << endl;

               

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

                                if (sList[i].testScore == high)

                                                out << sList[i].studentLName << ", " << sList[i].studentFName << endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote