I'm trying to read how many lines there are in the file to get the number of records and compare that to the header record number indicated by the file. The readInput
function works when returning true, but when removing lines in the file to make the function return false, it crashes the program.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Info
{
string name;
string idNum;
int testNum;
int* tests;
int average;
char grade;
};
int headRec(ifstream&);
Info* allocate(int);
bool readInput(Info* , ifstream& , int);
void displayData(Info* , int);
void deallocate(Info* , int);
int main()
{
int record;
string filename;
Info* data;
cout << "Please enter the input file name --> ";
cin >> filename;
ifstream inputFile(filename.c_str());
while (!inputFile)
{
cout << filename << " cannot be found. Input another file name" << endl;
cout << "Please enter the input file name --> ";
cin >> filename;
inputFile.clear();
inputFile.open(filename.c_str());
}
record = headRec(inputFile);
if (record == 0)
{
cout << "Error. Program Ending";
exit(0);
}
data = allocate(record);
if (data == nullptr)
{
cout << "Memory Allocation Error. Program Ending";
exit(0);
}
if (readInput(data, inputFile, record) == false)
{
cout << "Program Ending";
}
displayData(data, record);
deallocate(data, record);
cout << "\nPress Enter to end --> ";
cin.ignore();
cin.ignore();
return 0;
}
int headRec(ifstream& inputFile)
{
int record = 0;
inputFile >> record;
if (record < 5)
{
return record = 0;
}
return record;
}
Info* allocate(int record)
{
Info* data = new Info[record];
return data;
}
bool readInput(Info* data, ifstream& inputFile, int record)
{
int total = 0, end = 0;
char c;
for (int i = 0; i < record; i++)
{
inputFile >> data[i].name;
inputFile >> data[i].idNum;
inputFile >> data[i].testNum;
data[i].tests = new int[data[i].testNum];
for (int j = 0; j < data[i].testNum; j++)
{
inputFile >> data[i].tests[j];
total += data[i].tests[j];
}
inputFile.get(c);
data[i].average = total / data[i].testNum;
total = 0;
if (data[i].average < 60)
data[i].grade = 'F';
else if (data[i].average <= 70)
data[i].grade = 'D';
else if (data[i].average <= 80)
data[i].grade = 'C';
else if (data[i].average <= 90)
data[i].grade = 'B';
else
data[i].grade = 'A';
if (c == '\n')
end++;
}
cout << end;
cout << record;
if (end == record)
return true;
else
return false;
}
void displayData(Info* data, int record)
{
for (int i = 0; i < record; i++)
{
cout << "\nName: " << data[i].name;
cout << "\tID Number: " << data[i].idNum << endl;
cout << "Test Scores: ";
for (int j = 0; j < data[i].testNum; j++)
{
cout << data[i].tests[j] << " ";
}
cout << endl;
cout << "Average: " << data[i].average;
cout << "\tGrade: " << data[i].grade << endl;
}
}
void deallocate(Info* data, int record)
{
for (int i = 0; i < record; i++)
delete[] data[i].tests;
delete[] data;
}
This is what the file contains:
10
Mark 2345A78 6 78 67 98 34 78 69
Emily 3246B59 5 89 56 74 59 98
Yao 8942A85 7 98 75 69 89 85 78 95
Claudette 6587C69 5 56 45 63 75 60
Niklos 5874B58 6 69 68 52 69 75 74
Channah 4587B32 7 98 95 100 96 89 96 94
Parissa 8524C84 5 98 96 85 91 90
Rodrick 9856C65 5 56 75 84 65 75
Chance 5648A67 6 100 89 96 89 82 86
Melissa 8786C78 5 80 63 75 77 72
Niels 4854A78 6 48 56 85 65 42 75