Need Help Reading Data from Text File into an Array

I have no idea how to get started on this homework assignment and would really appreciate some assistance to help me get started:

"Create base class Person with atleast five data elements such as name, ssn, address, etc.
Create derived-class Student which derives from Person with atlease five different data elements that are only applicable to Students, such as student_id, gpa, etc.
Make sure you create a default construct, copy constructor, and atleast one more custom constructor for each.
Make sure you create accessors/mutators for each.

Write a method readClasses() that will open a file based on student_id:
For example if the students id is 12345 then a called called 12345.txt will contain the classes they have taken.
Sample file (Class Semester Grade):

C++ FA09 A
Java SP10 B

ReadClasses() should open this file and load the data into an array.

Write method PrintTranscript() which should print out all information about a student as as:

John Smith
123 Main Street
San Mateo, CA. 94123

Class Taken Semester Grade
=========== ======== =====
C++ FA09 A
Java SP10 B


Write a method called add class that will prompt user as follows:

Enter Class Code (user just one word, no spaces):
Enter Semester (SSYY, i.e. SU10, FA10, etc.):
Enter Letter Grade:

Example:

Enter Class Code (user just one word, no spaces): MySQL
Enter Semester (SSYY, i.e. SU10, FA10, etc.): FA09
Enter Letter Grade: B-

After getting this data from the user append it to the end of their file, i.e. 12345.txt


Write a main test function to test all of the above functionality, ie.

void main ()
{
Student s(12345, "test", "bob", "123 Main Street", "San Mateo, CA
94123");
s.readClasses(); // read from file: 12345.txt
s.printTranscript();

string classCode;
string semester;
string grade;
// Prompts here to get Class Taken, Semester, and Grade from user...


s.addClass(classCode, semester, grade);
s.readClasses();
s.printTranscript();
}"
why cant you just do what it says? or do you not know how to read from file, how to make arrays, etc? do you have a specific part that you are stuck at? just remember to take it step by step. it does look a bit scary at first sight, but if you think about it part by part its pretty simple. xD GL
Thanks for the response, blueberry. How do I read something like "C++ FA09 A" into an array?
You haven't answered any of blueberry's questions, really. What part is the problem?
I don't really understand how to read multiple classes with their associated terms and grades into an array. How would I go about making sure that "C++" "FA09" and "A" are read as different elements into the array?
Last edited on
why not just read it line by line like


opens 123456.txt
reads first line "C++" puts in array
reads second line "FA09" puts in array
reads third line "A", puts in array
Could you show me a coded example? Thanks a lot for the help!
That would be doing the assignment for you, though. :/
Have you looked at any tutorials on reading from a file?
I understand how to read from a file, but how do I get the code to only read until the blank space for each element?
ummmm.....so, this undebugged(and incomplete and bad, but i think it coveys the message right), dunno if this follows your project requirements either.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


struct courses {
string course;
string semester;
string grade;
} classes [10] //I think you can do this....

void readclasses(){
int count=0;
         if (file.good()||count <= 10){
          file >> classes[count].course; /*assuming that you already have a globally declared open file(you shouldn’t have globally          declared things), where the current line is the class name*/
         file >> classes[count].semester;
         file >> classes[count].grade;
         count++
    }
}



and the said file would be in this format

1
2
3
4
5
6
C++
FA09
A
Java
SP10
B


sorry for my bad code, and I bet I made some mistakes
Topic archived. No new replies allowed.