matthewmonkan
February 11th, 2008, 05:31 PM
Hey guys. I'm creating a Student class as shown below. I read in student data from a text file (this all works by the way as the class and its functions are written) into multiple Student objects (Student a, Student b, etc...). However, I need to create an array of pointers to student objects. I'm guaranteed in the data file that there is no more than 100 students, so I made this:
Student * studentArray[100];
But I have no idea how to go about reading in a list of data during runtime so that the data goes into each object and each object goes into an array. I'm looking through textbooks and online resources and am so lost....I would greatly appreciate any help. If you look at my code, all I know how to do is hardcode Student objects.
#include <iostream>
#include <cassert>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
//----- Student Class -----//
class Student
{
public:
Student() {};
Student( int s, string ln, string fn, char mn, string c, string st, string phone,
char gend, char yr, int cred, double gpa, string mjr );
void display();
private:
int studentID, credits;
char gender, middleName, year;
double gpa;
string city, firstName, lastName, major, phoneNumber, state;
};
Student::Student ( int s, string ln, string fn, char mn, string c, string st, string phone,
char gend, char yr, int cred, double g, string mjr )
{
studentID = s;
lastName = ln;
firstName = fn;
middleName = mn;
city = c;
state = st;
phoneNumber = phone;
gender = gend;
year = yr;
credits = cred;
gpa = g;
major = mjr;
}
void Student::display()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << endl;
cout << studentID << " " << lastName << " " << firstName << " " << middleName << endl;
cout << city << " " << state << endl;
cout << phoneNumber << endl;
cout << gender << " " << year << " " << credits << " " << gpa << " " << endl;
cout << major << endl;
}
//----- End of Student Class -----//
int main()
{
// 1. Prompt user for name of file containing the student data.
cout << "Enter the name for the input file: ";
string inputFileName;
getline(cin, inputFileName);
ifstream fin;
fin.open(inputFileName.data());
assert( fin.is_open() );
int studentID, credits;
char gender, middleName, year;
double gpa;
string city, firstName, lastName, major, phoneNumber, state;
Student * studentArray[100];
fin >> studentID >> lastName >> firstName >> middleName >> city >> state
>> phoneNumber >> gender >> year >> credits >> gpa >> major;
Student a (studentID, lastName, firstName, middleName, city, state,
phoneNumber, gender, year, credits, gpa, major);
fin >> studentID >> lastName >> firstName >> middleName >> city >> state
>> phoneNumber >> gender >> year >> credits >> gpa >> major;
Student b (studentID, lastName, firstName, middleName, city, state,
phoneNumber, gender, year, credits, gpa, major);
fin >> studentID >> lastName >> firstName >> middleName >> city >> state
>> phoneNumber >> gender >> year >> credits >> gpa >> major;
Student c (studentID, lastName, firstName, middleName, city, state,
phoneNumber, gender, year, credits, gpa, major);
a.display();
b.display();
c.display();
// 2. Read in file and store contents in an array of pointers to
// student objects. How do I do this?
return 0;
}
The data file I read from looks like this:
(Every five lines there is a new student)
10103 Johnson, James L
Waupun, Wisconsin
7345229
M 1 15 3.15
ENGR
10104 Andrews, Peter J
Grand-Rapids, Michigan
9493301
M 2 42 2.78
CPSC
10110 Peters, Andrew J
Lynden, Washington
3239550
M 5 63 2.05
ART
10113 VandenVander, Vanessa V
Fremont, Michigan
5509237
F 4 110 3.74
HIST
10126 Aristotle, Alice A
Chino, California
3330861
F 3 78 3.10
PHIL
The whole list has about 60 or so students.
If I need to clarify anything, feel free to ask; I'll be monitoring every 10 minutes or so...but please, I really need some help...I'm just lost. Thanks!
Student * studentArray[100];
But I have no idea how to go about reading in a list of data during runtime so that the data goes into each object and each object goes into an array. I'm looking through textbooks and online resources and am so lost....I would greatly appreciate any help. If you look at my code, all I know how to do is hardcode Student objects.
#include <iostream>
#include <cassert>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
//----- Student Class -----//
class Student
{
public:
Student() {};
Student( int s, string ln, string fn, char mn, string c, string st, string phone,
char gend, char yr, int cred, double gpa, string mjr );
void display();
private:
int studentID, credits;
char gender, middleName, year;
double gpa;
string city, firstName, lastName, major, phoneNumber, state;
};
Student::Student ( int s, string ln, string fn, char mn, string c, string st, string phone,
char gend, char yr, int cred, double g, string mjr )
{
studentID = s;
lastName = ln;
firstName = fn;
middleName = mn;
city = c;
state = st;
phoneNumber = phone;
gender = gend;
year = yr;
credits = cred;
gpa = g;
major = mjr;
}
void Student::display()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << endl;
cout << studentID << " " << lastName << " " << firstName << " " << middleName << endl;
cout << city << " " << state << endl;
cout << phoneNumber << endl;
cout << gender << " " << year << " " << credits << " " << gpa << " " << endl;
cout << major << endl;
}
//----- End of Student Class -----//
int main()
{
// 1. Prompt user for name of file containing the student data.
cout << "Enter the name for the input file: ";
string inputFileName;
getline(cin, inputFileName);
ifstream fin;
fin.open(inputFileName.data());
assert( fin.is_open() );
int studentID, credits;
char gender, middleName, year;
double gpa;
string city, firstName, lastName, major, phoneNumber, state;
Student * studentArray[100];
fin >> studentID >> lastName >> firstName >> middleName >> city >> state
>> phoneNumber >> gender >> year >> credits >> gpa >> major;
Student a (studentID, lastName, firstName, middleName, city, state,
phoneNumber, gender, year, credits, gpa, major);
fin >> studentID >> lastName >> firstName >> middleName >> city >> state
>> phoneNumber >> gender >> year >> credits >> gpa >> major;
Student b (studentID, lastName, firstName, middleName, city, state,
phoneNumber, gender, year, credits, gpa, major);
fin >> studentID >> lastName >> firstName >> middleName >> city >> state
>> phoneNumber >> gender >> year >> credits >> gpa >> major;
Student c (studentID, lastName, firstName, middleName, city, state,
phoneNumber, gender, year, credits, gpa, major);
a.display();
b.display();
c.display();
// 2. Read in file and store contents in an array of pointers to
// student objects. How do I do this?
return 0;
}
The data file I read from looks like this:
(Every five lines there is a new student)
10103 Johnson, James L
Waupun, Wisconsin
7345229
M 1 15 3.15
ENGR
10104 Andrews, Peter J
Grand-Rapids, Michigan
9493301
M 2 42 2.78
CPSC
10110 Peters, Andrew J
Lynden, Washington
3239550
M 5 63 2.05
ART
10113 VandenVander, Vanessa V
Fremont, Michigan
5509237
F 4 110 3.74
HIST
10126 Aristotle, Alice A
Chino, California
3330861
F 3 78 3.10
PHIL
The whole list has about 60 or so students.
If I need to clarify anything, feel free to ask; I'll be monitoring every 10 minutes or so...but please, I really need some help...I'm just lost. Thanks!