Need this program to check for the input file to see if it exists or not. If it
ID: 3627861 • Letter: N
Question
Need this program to check for the input file to see if it exists or not. If it does not, print an error "Cannot find input file" Terminating program, then close the program. This is all that needs to be done. Please modify this only. Some type of error of this nature anyway.
//header files
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//If 20 students took the test
const int numStudents = 20;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
//A function to read the students data into the array.
void getData(ifstream &inFile, studentType test1[numStudents])
{
for(int i = 0; i < numStudents; i++)
{
inFile >> test1[i].studentFName >> test1[i].studentLName >> test1[i].testScore; //assuming data file will be like <first> <last> <score>
}
inFile.close (); //closing data file
}
//A function to assign the relevant grade to each student.
void getLetterGrades(studentType test1[numStudents])
{
for(int i = 0; i < numStudents; i++) //figuring out the student letter grades
{
//compares the testscore to each 90+, 80-89, 70-79, 60-69, 60- brackets to determine letter grade
if (test1[i].testScore < 60)
test1[i].grade = 'F';
else if (test1[i].testScore < 70)
test1[i].grade = 'D';
else if (test1[i].testScore < 80)
test1[i].grade = 'C';
else if (test1[i].testScore < 90)
test1[i].grade = 'B';
else
test1[i].grade = 'A';
}
}
//A function to find the highest testscrore.
int findHighest(studentType test1[numStudents])
{
int top = test1[0].testScore; //sets the highest student to the first element
for(int i = 1; i < numStudents; i++)
{
if(top < test1[i].testScore) top = test1[i].testScore; //if the current element is higher than the current highest then the highest is the current element
}
return top; //returns the highest scoring student
}
void printTests(ofstream &outFile, studentType test1[numStudents])
{
//align the names to the left
cout << "Please see file Ch10_Ex2Out.txt for the results."<<endl;
//align the names to the left
outFile.setf(ios::left);
outFile << setw(23) << "Student Name";
outFile.unsetf(ios::left);
//align the test headings to the right
outFile.setf(ios::right);
outFile << setw(10) << "Test Score" << setw(7) << "Grade" << endl;
for(int i = 0; i < numStudents; i++)
{
outFile.unsetf(ios::right);
outFile.setf(ios::left); //align the names to the left
outFile << setw(23) << test1[i].studentLName + ", " + test1[i].studentFName;
outFile.unsetf(ios::left);
outFile.setf(ios::right); //align the tests to the right
outFile << setw(8) << test1[i].testScore << setw(7) << test1[i].grade << endl;
}
}
//A function to print the names of the students having the highest test score.
void printHighest(ofstream &outFile, studentType test1[numStudents], int highest)
{
outFile << " Highest Test Score: " << highest << endl; //prints out the highest test score
outFile <<"Students having the highest test score:"<<endl;
for(int i = 0; i < numStudents; i++)
{
if(test1[i].testScore == highest)
{
outFile << test1[i].studentLName + ", " + test1[i].studentFName << endl; //prints student with the highest test score
}
}
}
int main()
{
studentType test1[numStudents]; //test scores array made for students
ifstream inFile;
ofstream outFile;
inFile.open("Ch10_Ex2Data.txt");
outFile.open("Ch10_Ex2Out.txt"); //opens input and output for reading/writing
getData(inFile, test1); //fills the array up with names and scores
getLetterGrades(test1); //gives a letter grade to each student's score
printTests(outFile, test1); //prints the results
printHighest(outFile, test1, findHighest(test1)); //prints the highest scorers
outFile.close (); //closing output file
system("pause");
return 0;
}
Explanation / Answer
please rate - thanks
//header files
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//If 20 students took the test
const int numStudents = 20;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
//A function to read the students data into the array.
void getData(ifstream &inFile, studentType test1[numStudents])
{
for(int i = 0; i < numStudents; i++)
{
inFile >> test1[i].studentFName >> test1[i].studentLName >> test1[i].testScore; //assuming data file will be like <first> <last> <score>
}
inFile.close (); //closing data file
}
//A function to assign the relevant grade to each student.
void getLetterGrades(studentType test1[numStudents])
{
for(int i = 0; i < numStudents; i++) //figuring out the student letter grades
{
//compares the testscore to each 90+, 80-89, 70-79, 60-69, 60- brackets to determine letter grade
if (test1[i].testScore < 60)
test1[i].grade = 'F';
else if (test1[i].testScore < 70)
test1[i].grade = 'D';
else if (test1[i].testScore < 80)
test1[i].grade = 'C';
else if (test1[i].testScore < 90)
test1[i].grade = 'B';
else
test1[i].grade = 'A';
}
}
//A function to find the highest testscrore.
int findHighest(studentType test1[numStudents])
{
int top = test1[0].testScore; //sets the highest student to the first element
for(int i = 1; i < numStudents; i++)
{
if(top < test1[i].testScore) top = test1[i].testScore; //if the current element is higher than the current highest then the highest is the current element
}
return top; //returns the highest scoring student
}
void printTests(ofstream &outFile, studentType test1[numStudents])
{
//align the names to the left
cout << "Please see file Ch10_Ex2Out.txt for the results."<<endl;
//align the names to the left
outFile.setf(ios::left);
outFile << setw(23) << "Student Name";
outFile.unsetf(ios::left);
//align the test headings to the right
outFile.setf(ios::right);
outFile << setw(10) << "Test Score" << setw(7) << "Grade" << endl;
for(int i = 0; i < numStudents; i++)
{
outFile.unsetf(ios::right);
outFile.setf(ios::left); //align the names to the left
outFile << setw(23) << test1[i].studentLName + ", " + test1[i].studentFName;
outFile.unsetf(ios::left);
outFile.setf(ios::right); //align the tests to the right
outFile << setw(8) << test1[i].testScore << setw(7) << test1[i].grade << endl;
}
}
//A function to print the names of the students having the highest test score.
void printHighest(ofstream &outFile, studentType test1[numStudents], int highest)
{
outFile << " Highest Test Score: " << highest << endl; //prints out the highest test score
outFile <<"Students having the highest test score:"<<endl;
for(int i = 0; i < numStudents; i++)
{
if(test1[i].testScore == highest)
{
outFile << test1[i].studentLName + ", " + test1[i].studentFName << endl; //prints student with the highest test score
}
}
}
int main()
{
studentType test1[numStudents]; //test scores array made for students
ifstream inFile;
ofstream outFile;
inFile.open("Ch10_Ex2Data.txt");
if( inFile.fail()) //is it ok?
{ cout<<"input file did not open "Ch10_Ex2Data.txt" please check it exists ";
system("pause");
return 1;
}
outFile.open("Ch10_Ex2Out.txt"); //opens input and output for reading/writing
getData(inFile, test1); //fills the array up with names and scores
getLetterGrades(test1); //gives a letter grade to each student's score
printTests(outFile, test1); //prints the results
printHighest(outFile, test1, findHighest(test1)); //prints the highest scorers
outFile.close (); //closing output file
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.