Strange error

I've compiled this code in 2 different computers, Windows 7 and Fedora 18 and I get the same error. Here is the code:

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
// Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;

class Student
{
public:
	Student();
	Student(const string &firstName, char mi, const string &lastName, int score);
	void setFirstName(const string &s);
	void setMi(char mi);
	void setLastName(const string &s);
	void setScore(int score);
	string getFirstName() const;
	char getMi() const;
	string getLastName() const;
	int getScore() const;

private:
	string firstName;
	char mi;
	string lastName;
	int score;
};

#endif 


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
// Student.cpp
#include "Student.h"

//Construct a default stdent
Student::Student()
{
}

// Construct a Student object with specified

Student::Student(const string &firstName, char mi, const string &lastName, int score)
{
	setFirstName(firstName);
	setMi(mi);
	setLastName(lastName);
	setScore(score);
}
void Student::setFirstName(const string &s)
{
	firstName = s;
}

void Student::setMi(char mi)
{
	this->mi = mi;
}

void Student::setLastName(const string &s)
{
	lastName = s;
}

void Student::setScore(int score)
{
	this->score = score;
}

string Student::getFirstName() const
{
	return firstName;
}

char Student::getMi() const
{
	return mi;
}

string Student::getLastName() const
{
	return lastName;
}

int Student::getScore() const
{
	return score;
}


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
// StudentUseClass.cpp
#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;

void displayStudent(const Student &student)
{
	cout << student.getFirstName() << " ";
	cout << student.getMi() << " ";
	cout << student.getLastName() << " ";
	cout << student.getScore() << endl;
}

int main()
{
	fstream students; // Creates stream object
	students.open("database.txt", ios::in | ios::binary);

	Student newStudent;

	students.read(reinterpret_cast<char*> (&newStudent), sizeof(Student));

	displayStudent(newStudent);

	students.close();

	return 0;
}


Basically, this program opens a file called "database.txt" and outputs its content on the screen. It read first name, middle initial, last name and grade and displays it on the screen. That's it. Here is my "database.txt" file:

Anderson O Fernandes 99
Valentina I Castillo 98


Here is a screenshot of the error:

http://img198.imageshack.us/img198/7074/screenshotfpw.png

Can anybody help?
Last edited on
Your class contains members of type std::string. You may not store an object of the class in a binary file the way you do. And you may not read an object of the class such a way. In this case members of type std::string have undefined values.
How would I fix it? All I really need to do is to store first names and last names in string, middle names in whatever data type and scores as doubles and show it in console.
It would be simpler to store objects of the class in a text file with using operator >>. Or you can store in a binary file but before storing an object you should convert members of type std::string to ordinary character arrays. That is you should store not the members of type std::string but their values.
Last edited on
Okay, here is what I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream input("database.txt");
	string firstName;
	char mi;
	string lastName;
	int score;

	while (!input.eof())
	{
		input >> firstName >> mi >> lastName >> score;
		cout << firstName << " " << mi << " " << lastName << " " <<score << endl;
	}

	input.close();

	return 0;
}


It does its job... Thanks for the help, vlad from moscow.
You can write a corresponding method in your class to read data from an input stream and use this method.
Last edited on
Topic archived. No new replies allowed.