I'm currently writing a program that needs to read a input file and populate a set of Student objects. Then it will display the objects, and perform some basic loop and function-based processing. I'm still new to C++, especially with file handling and anything object-oriented. Any step by step help I get will be greatly appreciated.
Input file data:
John Daniel Fuller E23123456034Malic Stephen Browscent W03023142039Richard E. Pollidence E02782635021Frank William Lomo E09912376022Colin Frankson R23234556023James Theodore Jackson D02323245059Dave Gerald Mungbean F12333221042Jim Waymo Hilbert W02785634055Barb C Bowie W02833546030Jim D Carlo S22876543033
Processing the input:
Open the input file. Check for successful open. If the open failed, display an error message and return with value 1.
Use a properly-structured loop to read the input file until EOF.
Use the .read(reinterpret_cast) function to read each record into a character array large enough to hold the entire record.
For each input file record, dynamically allocate and populate a Student variable. Note that to populate some of the Student fields, you'll need to perform some type of conversion. Use a pointer array to manage all the created student variables.
Assume that there will not be more than 99 input records, so the size of the student variable pointer array will be 100. Use a "global" variable to define the size of the pointer array.
Display the Student objects:
When you hit EOF on the input file, close it, and display the entire list of students. Go through the student objects - do not create the report while you're reading input records. Example output is below.
Note that the 5th student (Colin Frankson) doesn't have a middle initial/name. Leave it blank in the output.
My code so far:
#include <iostream>
#include <fstream>
using namespace std;
const int SIZE = 100;
#ifndef Student_h
#define Student_h
struct Student
{
char first_name[10];
char middle_int[10];
char last_name[20];
char campus_code;
char student_ID[8];
char age[3];
};
#endif
int main()
{
ifstream inputFile;
inputFile.open("Students.txt", ios::binary);
if(!inFile.is_open())
{
cout << "Unable to open file!";
return 1;
}
else
{
cout << "File successfully open!\n\n";
}
Student *arr[SIZE];
Student *st = nullptr;
int total = 0;
Student s;
inFile.read(reinterpret_cast<char *> (&s), sizeof(s));
// pseudocode:
while(!inputFile.eof())
{
// dynamically allocate space for new student variables
st = new Student();
// populate the student variables from the input
st = &s;
// read the input record
}
inFile.close();
// Display the student variables with appropriate column headers and
// formatting to make it readable
/*
for(int i = 0; i < total; i++)
{
cout << "First Name" << "\t" << "MI" << "\t" << "Last Name" << "\t" << "Campus Code" << "\t" << "Student ID" << "\t" << "Age" << endl;
cout << "===================================================================================================================" << endl;
}
*/
return 0;
}