Write a program that reads students’ names followed by theirtest scores. The program should output each students’ namefollowed by the test scores and relevant grade. It should also findand print the highest test score and the name of the studentshaving the highest test score.
Student data should be stored in a struct variable of typeStudentType, which had four components:studentFName and studentLName of typestring,
testScore of type int (testScore isbetween 0 and 100), and
grade of type char.
Suppose that the data has 20 students. Use an array of 20components of type studentType.
Program must contain at least the following functions:
A function to read the students data into the array
A function to assign the relevant grade to each student
A function to print the names of the students having the highesttest score.
Program must output each students name in this form: Last name,followed by a comma, followed by a space, followed by first name,and the name must be left justified. Moreover, other than declaringthe variables and opening the input and out files, the functionmain should only be a collection of function calls.
The following program compiles without erors, but in the terminal window it says file cannot be opened. Please tell me where to edit and what to edit
bool openinfile(ifstream& inFile, char filename[]);
void openoutfile(ofstream& outFile, char filename[]);
int readData(ifstream& inFile, studentType students[]);
void getgrade(studentType students[], int n);
void printData(ofstream& outFile, studentType students[], int n);
void highScore(ofstream& outFile, studentType students[], int n);
int main()
{
bool good;
int n;
ifstream infile;
ofstream outfile;
good = openinfile(infile, "Afham.txt");
if (good)
{
openoutfile(outfile, "output.txt");
n = readData(infile, students);
getgrade(students, n);
printData(outfile, students, n);
highScore(outfile, students, n);
outfile.close();
infile.close();
}
system("pause");
return 0;
}
void openoutfile(ofstream& outFile, char filename[])
{
outFile.open(filename);
}
bool openinfile(ifstream& infile, char filename[])
{
infile.open(filename);
if (infile.fail())
{
cout << "file did not open please check it\n";
system("pause");
return false;
}
return true;
}
int readData(ifstream& inFile, studentType students[])
{
int n = 0;
inFile >> students[n].studentFName >> students[n].studentLName >> students[n].testScore;
while (inFile)
{
n++;
inFile >> students[n].studentFName >> students[n].studentLName >> students[n].testScore;
}
return n;
}
void getgrade(studentType students[], int n)
{
int i;
for (i = 0; i<n; i++)
switch ((int)(students[i].testScore / 10))
{
case 10:
case 9: students[i].grade = 'A';
break;
case 8: students[i].grade = 'B';
break;
case 7: students[i].grade = 'C';
break;
case 6: students[i].grade = 'D';
break;
default: students[i].grade = 'F';
break;
}
}
void printData(ofstream& out, studentType students[], int n)
{
out << "name score grade\n";
for (int i = 0; i<n; i++)
{
out << left << students[i].studentLName << "," << students[i].studentFName << " \t";
out << students[i].testScore << "\t" << students[i].grade << endl;
}
}
void highScore(ofstream& out, studentType students[], int n)
{
int max = 0, i;
for (i = 1; i<n; i++)
if (students[i].testScore>students[max].testScore)
max = i;
out << "Highest test score: " << students[max].testScore << endl;
out << "Students receiveing highest score:" << endl;
for (i = 0; i<n; i++)
if (students[i].testScore == students[max].testScore)
out << students[i].studentLName << "," << students[i].studentFName << endl;
Your program contains the correct instructions to the user in function openinfile(),
cout << "file did not open please check it\n";
That is to say, the program could not find the file "Afham.txt" - you need to check that it exists, and is in the correct directory. Usually this will be the same directory as that of the executable program (not where you put the source code).
I did everything, It still doesn't display anything. So in "Afham.txt" I have all the student information like names and test scores and it is in the correct directory. What do I do with output.txt ?
Bob Smith 87
Mike Christy 90
Steven Michaels 33
Michael John 78
Amy Clark 99
Nicole Hasty 91
Bob Robert 89
Chris Nash 77
Chris Thomas 67
Joe Tea 93
Bob Young 87
Mike Stevenson 95
Trent Fiore 70
Anthony Joe 82
Matt Coffee 90
Mark Tomlinson 89
Phil Rope 94
Mike Stopper 88
Mark Robinson 78
Betty Lee 77
name score grade
Smith,Bob 87 B
Christy,Mike 90 A
Michaels,Steven 33 F
John,Michael 78 C
Clark,Amy 99 A
Hasty,Nicole 91 A
Robert,Bob 89 B
Nash,Chris 77 C
Thomas,Chris 67 D
Tea,Joe 93 A
Young,Bob 87 B
Stevenson,Mike 95 A
Fiore,Trent 70 C
Joe,Anthony 82 B
Coffee,Matt 90 A
Tomlinson,Mark 89 B
Rope,Phil 94 A
Stopper,Mike 88 B
Robinson,Mark 78 C
Lee,Betty 77 C
Highest test score: 99
Students receiveing highest score:
Clark,Amy
Press any key to continue . . .