Class functions are not working

Why does my code not execute correctly?

HeartRates.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <cmath>

class HeartRates
{
public:

	void setFirstName()
	{
		std::cin >> firstName;
	}

	void setLastName()
	{
		std::cin >> lastName;
	}

	void setBirthMonth()
	{
		loopVar = true;
		while (loopVar == true)
		{
			std::cin >> intInput;
			if (0 < intInput && intInput < 13)
			{
				birthMonth = intInput;
				loopVar = false;
			}
			else
			{
				std::cout << "Invalid input.  Enter month of birth.";
			}
		}
		
	}

	void setBirthMday()
	{
		loopVar = true;
		while (loopVar == true)
		{
			std::cin >> intInput;
			if (0 < intInput && intInput < 32)
			{
				birthMday = intInput;
				loopVar = false;
			}
			else
			{
				std::cout << "Invalid input.  Enter day of birth.";
			}
		}
	}

	void setBirthYear()
	{
		loopVar = true;
		while (loopVar == true)
		{
			std::cin >> intInput;
			if (1000 < intInput && intInput < 2018)
			{
				birthYear = intInput;
				loopVar = false;
			}
			else
			{
				std::cout << "Invalid input.  Enter year of birth.";
			}
		}
	}

	std::string getFirstName()
	{
		return firstName;
	}

	std::string getLastName()
	{
		return lastName;
	}

	int getBirthMonth()
	{
		return birthMonth;
	}

	int getBirthMday()
	{
		return birthMday;
	}

	int getBirthYear()
	{
		return birthYear;
	}

	int getMaxHeart()
	{
		std::cout << "age = " << age << "." << std::endl;
		maxHeart = 220 - age;
		
		return maxHeart;
	}

	int getMaxTargetHeart()
	{
		maxTargetHeart = std::round(maxHeart * 0.85);
		return maxTargetHeart;
	}

	int getMinTargetHeart()
	{
		minTargetHeart = maxHeart * 0.5;
		return minTargetHeart;
	}
	
	int ageCalculation(int cMonth, int cMday, int cYear)
	{
		age = cYear - birthYear;
		if (cMonth < birthMonth || (cMonth == birthMonth && cMday < birthMday))
		{
			age--;
		}
		
		return age;
	}
	

private:
	std::string firstName;
	std::string lastName;
	std::string stringInput;
	int birthMonth;
	int birthMday;
	int birthYear;
	int intInput;
	int age;
	int maxHeart;
	int maxTargetHeart;
	int minTargetHeart;
	bool loopVar;


};


main.cpp
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
#include <ctime>
#include <iostream>
#include "HeartRates.h"



int main()
{

	// Get current date
	time_t t = time(0);
	struct tm * now = localtime(&t);
	int currentYear = now->tm_year + 1900;
	int currentMonth = now->tm_mon + 1;
	int currentMday = now->tm_mday;

	HeartRates subject01;

	std::cout << "Enter the name of the subject." << std::endl
		<< "First name: ";
	subject01.setFirstName();
	std::cout << "Last name: ";
	subject01.setLastName();
	std::cout << std::endl << "Enter the numeric date of birth."
		<< std::endl << "Month: ";
	subject01.setBirthMonth();
	std::cout << "Day: ";
	subject01.setBirthMday();
	std::cout << "Year: ";
	subject01.setBirthYear();

	std::cout << subject01.getFirstName() << " " 
		<< subject01.getLastName() << " was born " 
		<< subject01.getBirthMonth() << "/"
		<< subject01.getBirthMday() << "/"
		<< subject01.getBirthYear() << " and is "
		<< subject01.ageCalculation(currentMonth, currentMday, currentYear) << " years old."
		<< std::endl
		<< "Maximum heart rate: " << subject01.getMaxHeart() << std::endl
		<< "Target heart rate is between " << subject01.getMinTargetHeart()
		<< " and " << subject01.getMaxTargetHeart() << std::endl;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
Enter the name of the subject.
First name: John
Last name: Smith

Enter the numeric date of birth.
Month: 8
Day: 20
Year: 1980
age = -858993460.
John Smith was born 8/20/1980 and is 36 years old.
Maximum heart rate: 858993680
Target heart rate is between -429496730 and -730144441


Everything works fine until after I enter in the year of birth.
I finally figured it out, after 30 minutes of staring at it!!!

http://stackoverflow.com/questions/11603894/called-a-function-with-cout-statement-inside-a-cout-statement

you're placing functions inside of functions inside of functions when you do a long chain with the cout and << operators

when you say
"Maximum heart rate: " << subject01.getMaxHeart() << std::endl
subject01.getMaxheart() has to get evaluated first so you're gonna get an out put for age before it's actually calculated and then anything with age is gonna be messed up

as a rule of thumb you want "get" functions to only return values and nothing else.
they shouldn't change or set anything.
Topic archived. No new replies allowed.