Printing object array incorrectly

I dont know why my program keeps printing the array like this!

records.txt
123 Jose Garcia 99
345 Luis Garcia 87
234 Jazlyn Soriano 79
456 Carter Sander 95
567 Natalia Soto 67
789 Isabel Santana 80

It keeps printing
12399123 Jose Garcia 99
34587345 Luis Garcia 87
23479234 Jazlyn Soriano 79
45695456 Carter Sander 95
56767567 Natalia Soto 67
78980789 Isabel Santana 80

It is adding the grade to the id and then the id again.
help?

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
74
75
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Student
{
private:
	int id;
	string firstName;
	string lastName;
	int examGrade;
public:
	Student();
	Student(int, string, string, int);
	void setall(int, string, string, int);
};

int main()
{
	Student *records;
	records = new Student[6];
	int id;
	string firstName;
	string lastName;	
	int examGrade;

	fstream gradebook;
	gradebook.open("C://Users//Esmeralda//Desktop//Records.txt",ios::in);
	if (gradebook.fail())
		{
			cout << "Error opening file" << endl;
			exit(0);
		}
	int i=0;
	while (!gradebook.eof() && i<6)
	{
		gradebook >> id >> firstName >> lastName >> examGrade;
		records[i].Student::Student(id, firstName, lastName, examGrade);
		cout << id << "\t" <<  firstName << "\t" << lastName << "\t" <<  examGrade << endl;
		i++;
	}

	gradebook.close();
	delete [] records;
	system("pause");
	return 0;
}
Student::Student()
{
	id= 0;
	firstName=" ";
	lastName=" ";
	examGrade=0;
}
Student::Student(int theId, string theFirstName, string theLastName, int theGrade)
{
	setall(theId, theFirstName, theLastName, theGrade);
}
void Student::setall(int theId, string theFirstName, string theLastName, int theGrade)
{
	if(theId>999 || theId<100)
		exit(1);
	else
		cout << theId;

	firstName = theFirstName;

	lastName = theLastName;

	if(theGrade>101 || theGrade<0)
		exit(1);
	else
		cout << theGrade;
}
Last edited on
What compiler do you use?

line 39 should not (Doh!) compile. You may change it like:

records[i] = Student(id, firstName, lastName, examGrade);

or you're using records[i] on line 38 directly:

gradebook >> records[i].id >> records[i].firstName >> records[i].lastName >> records[i].examGrade;
Last edited on
I swear I kept trying to put it as
 
 records[i] = Student(id, firstName, lastName, examGrade);


yesterday and it wouldn't work. I guess I had a typo in there somewhere.

Thanks for that!

But now, im wondering why it keeps printing like this:
12399123 Jose Garcia 99
34587345 Luis Garcia 87
23479234 Jazlyn Soriano 79
45695456 Carter Sander 95
56767567 Natalia Soto 67
78980789 Isabel Santana 80

Rather than like this:
123 Jose Garcia 99
345 Luis Garcia 87
234 Jazlyn Soriano 79
456 Carter Sander 95
567 Natalia Soto 67
789 Isabel Santana 80

But now, im wondering why it keeps printing like this:
You find the reason on line 65 and 74.

By the way: You do not set id or examGrade in setall(...)
Thanks! i got it!
Topic archived. No new replies allowed.