Constructor and overload the operator help!

hi i was wondering if someone can help me in writing this:
1. Constructor Student() to initialized name, course and phone_no to empty string and set id to 0.

2. Write function ostream& operator << (ostream&, Student stu) to overload operator << to print Student object.

i wrote a bit of it but i dont know if its right

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  #ifndef Student_type
#define Student_type

#include    <cstring>
#include    <sstream>
#include    <iostream>
#include    <iomanip>
#include    "List.h"
#include    "Book.h"

using namespace std;

//student info
class Student {		
public:

	char name[30];	
        char id[10];
	char course[3];
	char phone_no[10];
	stringstream NAME, ID, COURSE, PHONE;
	string SName, SID, SCourse, SPhone;

	Student();
	friend ostream& operator<< (ostream&, Student);
};


Student::Student()
{
	NAME << name;
	NAME >> SName;
	cout << "Please enter the student name :" << endl;
	cin >> name;

	ID << id;
	ID >> SID;
	cout << endl << "Please enter theid student id :" << endl;
	cin >> id;

	bool setID(const int Sid) // returns true if successful
	{
		if (Sid < 1) // ID must be > 0
			return false;

		id = Sid;
		return true;
	}


	COURSE << course;
	COURSE >> SCourse;
retry:
	cout << endl << "Please enter the student course :" << endl;
	cin >> course;
	cin.ignore(256, '\n');

	if (course == 'CS')
	{

	}
	else if (course == 'IA')
	{

	}
	else if (course == 'CT')
	{

	}
	else if (course == 'CN')
	{

	}
	else if (course == 'IB')
	{

	}
	else
	{
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "please enter only CS, CT, IA, IB and CN only.";
		cout << endl << "Please enter the student course :" << endl;
		cin >> course;
		goto retry;
	}

	PHONE << phone_no;
	PHONE >> SPhone;
	cout << endl << "Please enter the student phone number :" << endl;
	cin >> phone_no;
	cout << endl;
	while (ispunct(phone_no))
	{
		cout << "ERROR: Invalid input!" << endl;
		cin.clear();
		cin.ignore(100, '\n');
		cin >> phone_no;
	}
	
}


ostream& operator << (ostream& out, Student stu)
{

}
to initialized name, course and phone_no to empty string and set id to 0.

If that is the goal, what do the lines 31-99 do? Do nothing, except what is stated.

I wonder what data members the class Student should have, because what it now has, looks suspicious.


Make the stu a const reference in operator<<

The code in line 105. Think what this following program should print:
1
2
3
4
5
6
int main() {
  Student foo;
  // code to change foo's member variables
  std::cout << foo << '\n';
  return 0;
}
Last edited on
Tq for the reply, now i got the idea how to do it.....yah the line 31-99 is totally wrong just ignore it, i will change it later.
Topic archived. No new replies allowed.