pointer object to str() does not return data

my problem occurs in : cout << p1->str();

line 68

I need information from the student account to be displayed after being inserted into the
tmp_variables in the main(), but they are not appearing. I don't know how to make it so
that this information will be stored in str(); or if it is that p1 -> str() isn't actually pointing
to it.


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
#include <iostream>
#include <sstream>

using namespace std;

class Person
{
	protected:
		string name,
			   address,
			   phone,
			   email; //variables for Student, Faculty, and Staff

	public:
		virtual string str() const {};

	Person() {};
	Person(string, string, string, string) {};
};
class Student:public Person
{
	protected:
		string status;
	public:

	string str() const {
		stringstream sso;
		sso << "Name: " << name << endl
		    << "Status: " << status << endl
		    << "Address: " << address << endl
		    << "Phone Number: " << phone << endl
		    << "Email: " << email << endl;
        return sso.str();
	}

	Student() {};
	Student(string, string, string, string, string) {};
};


int main()
{
    string s;
    string tmp_office;
    double tmp_salary;
    Person *p1;
    cout << "Enter the account type: ";
    getline(cin, s);
    if ( s == "Student")
	{
        string tmp_name, tmp_address, tmp_phone, tmp_email;
        cout << "Enter the person's name: ";
        getline(cin, tmp_name);
        cout << "Enter the person's address: ";
        getline(cin, tmp_address);
        cout << "Enter the person's phone: ";
        getline(cin, tmp_phone);
        cout << "Enter the person's email: ";
        getline(cin, tmp_email);

        if ( s == "Student" )	//STUDENT OPTION***
		{
            string tmp_status;
            cout << "Enter the student's status: ";
            getline(cin, tmp_status);
            p1 = new Student(tmp_status, tmp_name, tmp_address, tmp_phone, tmp_email); //5 student arguments

        cout << p1->str();
    }else
	{
		cout << "Unknown account type: " << s << endl;
	}
    return 0;
}

Last edited on
my problem occurs in : cout << p1->str();

No, it doesn't. Although this isn't your (current) problem, the Person::str function should either be pure virtual or actually return a string object.

None of your constructors do anything. I think I might look into those first.
I made the str() a pure virtual by making it = 0.

so I should add a body to the student constructor? In the instructions is this:

"... include a default constructor and a constructor that would initialize the data members to the values of its arguments ."

So I must initialize all the strings in my student constructor parameters.
Topic archived. No new replies allowed.