Read Data from text file to Class private members

I just need a really quick question. So I can tackle with normal class and how to do them but I am not sure how to actually read data from text file into 2 class private members of the class studentRecord. Really need some expert to help me on this one

This link will lead to how the CMD look like when I run the code
http://i.imgur.com/m1ik4oM.png

below is my code I'm currently typing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

#define a_row 10
#define a_col 6

fstream inputname, inputscore;

class studentRecord
{
private:
	
	string name[a_row];
	int grade[a_row][a_col];
	int sum;
	double average;
	char letter;
public:
	void readStudentData(string name, int grade);
	void printStudentData(void);
	/*int sumOfGrade(int grade, int sum);
	double averageGrade(int sum, double average);
	void letterGrade(double average, char letter);
	void printStudentData2(string name, int grade, int sum, double average, char letter);*/
};

void studentRecord::readStudentData(string name, int grade)
{
	inputscore.open("grade.txt");
	inputname.open("name.txt");
	if (!inputname) perror("File not found ");
	else
		cout << "File successfully opened" << endl;

	for (int r = 0; r < a_row; r++)
	{
		inputname >> studentRecord::name[r];
		for (int c = 0; c < a_col; c++)
		{
			inputscore >> studentRecord::grade[r][c];
		}
	}

}

void studentRecord::printStudentData(void)
{
	studentRecord student;
	for (int r = 0; r < a_row; r++)
	{
		cout << studentRecord::name << endl;
		for (int c = 0; c < a_col; c++)
		{
			cout << studentRecord::grade << endl;
		}
	}
}

//int studentRecord::sumOfGrade(int grade, int sum)
//{
//
//}

void main()
{
	studentRecord student;
	student.printStudentData();
	system("pause");
}
First main() must be defined to return an int: int main()

Second look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
void studentRecord::printStudentData(void)
{
	studentRecord student;
	for (int r = 0; r < a_row; r++)
	{
		cout << studentRecord::name << endl;
		for (int c = 0; c < a_col; c++)
		{
			cout << studentRecord::grade << endl;
		}
	}
}

Now please answer the following questions:

1. Why did you create a studentRecord named student? You never used this variable so what is it's purpose?

2a. Please tell me, in English, what you think this line is doing cout << studentRecord::name << endl;
2b. What is the purpose of the studentRecord:: part?
2c. What type of variable is name?

Now look at this snippet:
1
2
3
private:
	
	string name[a_row];

What is the purpose of the array?

1/ I was looking through that and has deleted that line, it was from an old code I was trying if it work but forgot to delete it

2/
a. That line I was trying to figured it out a way to printing out the name of the student Record after I read the data from text file to private member variable inside studentRecord

b. I was under assumption after looking through my note that it will print out the data of the private member of studentRecord.

c. Name variable is string.

The purpose of the array to make sure the number of name the code should be read to private member should only be 10.
2/
a. That line I was trying to figured it out a way to printing out the name of the student Record after I read the data from text file to private member variable inside studentRecord

b. I was under assumption after looking through my note that it will print out the data of the private member of studentRecord.


Okay to print a non-array member variable from a class member function all you need to do is print the variable, you don't need any qualifiers.
cout << name << endl;
A class member function has direct access to all class member variables.

c. Name variable is string.

No, name is an array of string not a string.


The purpose of the array to make sure the number of name the code should be read to private member should only be 10.

This doesn't make any sense. If you want 10 student names you should have an array of studentRecord not one studentRecord with 10 different student names. Every studentRecord only needs one name.

1
2
3
4
int main() // In C++ main() must be defined to return an int!
{
        // If you want 10 studentRecords this is where you need the array.
	studentRecord student[a_row];


So your class definition should look more like:
1
2
3
4
5
6
7
...
class studentRecord
{
private:
	
	string name; // No array needed here.
	int grade[a_col]; // Only need to know the number of grades so only a single dimension is required. 


Your print function should look more like:
1
2
3
4
5
6
7
8
9
void studentRecord::printStudentData(void)
{
	cout << name << endl;
	for (int c = 0; c < a_col; c++)
	{      // Print out each grade.
		cout << grade[c] << " ";
	}
        cout << endl;
}


Your "read" function should only read one student at a time (this function will need a major overhaul).

You should be opening your data files in main() (or some other non-member function) and then, in a loop, read each individual student's information.



Topic archived. No new replies allowed.