Classes, functions, set/get, oh my!

I'm very new to programming still and have an assignment for a distance education class of mine that I'm not grasping very well. I'm supposed to create a class named HealthProfile with member attributes first name, last name, gender, day of birth, month of birth, year of birth, height, weight. I'm supposed to request and return this information. I know I shouldn't be having this much trouble with such a simple task, but I'm stumped! This is what I have so far, and when I run it, it requests and returns the first and last name perfectly. When it asks for gender I enter "M" and press the enter key, the program immediately ends after. What gives?! I realize that I haven't even put anything in int main() yet, so that baffles me even further as to why it is running somewhat okay?

edit: I'm not asking for a complete answer either, just a hint as to what in the world is even happening :(


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
#include <iostream>
#include <string>
using namespace std;

class HealthProfile {
	string fName, lName, gender;
	int day, month, year, height, weight;

public:
	void set_fName (string);
	void set_lName (string);
	void set_gender (string);
	void set_day (int);
	void set_month (int);
	void set_year (int);
	void set_height (int);
	void set_weight (int);
};

void HealthProfile::set_fName (string fName) {
	cout << "First name: ";
	getline (cin,fName);
	return; 
};

void HealthProfile::set_lName (string lName) {
	cout << "Last name: ";
	getline (cin,lName);
	return;
};

void HealthProfile::set_gender (string gender) {
	cout << "Gender (M/F): ";
	cin >> gender;
	return;
};

void HealthProfile::set_day (int day) {
	cout << "DOB (Day): ";
	cin >> day;
	return;
};

void HealthProfile::set_month (int month) {
	cout << "DOB (Month): ";
	cin >> month;
	return;
};

void HealthProfile::set_year (int year) {
	cout << "DOB (Year): ";
	cin >> year;
	return;
};

void HealthProfile::set_height (int height) {
	cout << "Height (inches): ";
	cin >> height;
	return;
};

void HealthProfile::set_weight (int weight) {
	cout << "Weight (lbs): ";
	cin >> weight;
	return;
};
int main()

Last edited on
closed account (3pj6b7Xj)
Looks really good but think of classes as containers for encapsulating data. The getline stuff should be located outside the class for a program that uses it, you should be storing and manipulating data within the class not obtaining directly from an input program, I used to do that a lot. Its better for your class to spit out information and absorb information and to manipulate or use that information ... hey anything is possible.

If your issue with input was located outside the class yo wouldn't be concerned with the innards of the class itself but since you implemented getline within the class function, now you have to reach in and mess around with the class which could then mess other stuff and other stuff and you get the idea.
Topic archived. No new replies allowed.