Questions are below the code #include #include #include using namespace std; str
ID: 3556493 • Letter: Q
Question
Questions are below the code
#include
#include
#include
using namespace std;
struct Student
{
string name;
string ID;
int testNumber;
int *tests;
double average;
char grade;
};
int readHeader(ifstream&);
Student *allocate(int);
void readRecords(ifstream&, Student*, int);
void print(Student*, int);
void deallocate(Student*);
int main()
{
ifstream inFile;
int n = readHeader(inFile);
if (n < 5){
cout << n << " must be 5 or greater "
<< "Program Ending "
<< "Press Enter to end -->";
cin.ignore();
return 0;
}
Student *s = allocate(n);
readRecords(inFile,s, n);
for (int i = 0; i print(s, i);
}
deallocate(s);
inFile.close();
cout << "Press Enter to end -->";
cin.ignore();
return 0;
}
int readHeader(ifstream& inFile)
{
int a;
string filename;
cout << "Please enter the input filename --> ";
cin >> filename;
inFile.open(filename.c_str(), ifstream::in);
while(!inFile.is_open())
{
cout << filename << " cannot be found. Input another file name "
<< "Please enter the input filename --> ";
cin >> filename;
inFile.open(filename.c_str(), ifstream::in);
}
inFile >> a;
cout << " ";
return a;
}
Student *allocate(int a)
{
return new Student[a];
}
void readRecords(ifstream& inFile,Student *s, int a)
{
for (int i = 0; i int sum = 0;
inFile >> s[i].name;
inFile >> s[i].ID;
inFile >> s[i].testNumber;
s[i].tests = new int[s[i].testNumber];
for (int c = 0; c inFile >> s[i].tests[c];
sum += s[i].tests[c];
}
s[i].average = sum / (s[i].testNumber);
if (s[i].average >= 91){
s[i].grade = 'A';
}
else if (s[i].average >= 81){
s[i].grade = 'B';
}
else if (s[i].average >= 71){
s[i].grade = 'C';
}
else if (s[i].average >= 60){
s[i].grade = 'D';
}
else{
s[i].grade = 'F';
}
}
}
void print(Student *s, int i)
{
cout << "Name : " << s[i].name << " ID Number: " << s[i].ID << endl;
cout << "Test Scores: ";
for (int c = 0; c cout << s[i].tests[c] << " ";
}
cout << " Average: " << s[i].average << " Grade: " << s[i].grade;
cout << " ";
}
void deallocate(Student *s)
{
delete[] s;
}
some problems im having are
Missed something in main about the allocate function.
-A function to allocate the array of structures. This is called by main. The function returns the allocated dynamic array to main. Be sure to check in main that the allocation was successful. If not, end the program.
Press Enter does not work
Crashes on on of the error conditions described in the specification.
There may be more records than the header record number indicated. This is not considered an error. In this case, read the number of records indicated by the header record value. However, fewer records is a problem. This error should be returned to main and main should terminate the program.
A function to read the header record in the file. This record consists of a single value, the number of records in the file. The minimum number of records for the file is 5. If the value read from the file is less than 5, then return an error to main. This is considered a serious enough error for main to end the program.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct Student
{
string name;
string ID;
int testNumber;
int *tests;
double average;
char grade;
};
int readHeader(ifstream&);
Student *allocate(int);
void readRecords(ifstream&, Student*, int);
void print(Student*, int);
void deallocate(Student*);
int main()
{
ifstream inFile;
int n = readHeader(inFile);
if (n < 5){
cout << n << " must be 5 or greater "
<< "Program Ending "
<< "Press Enter to end -->";
cin.ignore(1,' ');
return 0;
}
Student *s = allocate(n);
readRecords(inFile,s, n);
for(int i=0;i<n;i++)
{
print(s, i);
}
deallocate(s);
inFile.close();
cout << "Press Enter to end -->";
cin.ignore();
return 0;
}
int readHeader(ifstream& inFile)
{
int a;
string filename;
cout << "Please enter the input filename --> ";
cin >> filename;
inFile.open(filename.c_str());
while(!inFile.is_open())
{
cout << filename << " cannot be found. Input another file name "
<< "Please enter the input filename --> ";
cin >> filename;
inFile.open(filename.c_str());
}
inFile >> a;
cout << " ";
return a;
}
Student *allocate(int a)
{
return new Student[a];
}
void readRecords(ifstream& inFile,Student *s, int a)
{
for (int i = 0; i<a;i++)
{
int sum = 0;
inFile >> s[i].name;
inFile >> s[i].ID;
inFile >> s[i].testNumber;
s[i].tests = new int[s[i].testNumber];
for (int c = 0;c<a;c++)
{
inFile >> s[i].tests[c];
sum += s[i].tests[c];
}
s[i].average = sum / (s[i].testNumber);
if (s[i].average >= 91){
s[i].grade = 'A';
}
else if (s[i].average >= 81){
s[i].grade = 'B';
}
else if (s[i].average >= 71){
s[i].grade = 'C';
}
else if (s[i].average >= 60){
s[i].grade = 'D';
}
else{
s[i].grade = 'F';
}
}
}
void print(Student *s, int i)
{
cout << "Name : " << s[i].name << " ID Number: " << s[i].ID << endl;
cout << "Test Scores: ";
for (int c=0;c<i;c++)
{
cout << s[i].tests[c] << " ";
cout << " Average: " << s[i].average << " Grade: " << s[i].grade;
cout << " ";
}
}
void deallocate(Student *s)
{
delete[] s;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.