"-858933460"

why is my program printing this strange negative number when it runs? It compiles just fine but for the age, target heart range, and maximum heart rate, it prints this strange value. Is it the location? If it is, how do i fix it?

MAIN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*#include <iostream>
#include <string>
using namespace std;
#include "heartRates.h"

int main()
{

	heartRates calc("Jennifer", "Morris", 8, 4, 1988, 2010);

	calc.displayInfo();

	return 0;
}*/




FUNCTION DEFINITION:
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
/*#include <iostream>
#include <string>
#include "heartRates.h"
using namespace std;

string heartRates::getFirst()
{
	return first;
}

string heartRates::getLast()
{
	return last;
}

int heartRates::getMonth()
{
	return month;
}

int heartRates::getDay()
{
	return day;
}

int heartRates::getYear()
{
	return year;
}

int heartRates::getcuryear()
{
	return c;
}

int heartRates::getAge()
{
	return age;
}

int heartRates::getMaximumHeartRate()
{
	return max;
}

int heartRates::getTargetHeartRate(int max)
{
	return hi;
	return lo;
}

void heartRates::setAge(int age)
{
	age = int(c - y);
}

void heartRates::setMaximumHeartRate(int max)
{
	max = 220 - age;
}
void heartRates::setTargetHeartRate(int max)
{
	hi = int(max * .85);
	lo = int(max * .5);
}

void heartRates::setFirst(string f)
{
	first = f;
}

void heartRates::setLast(string l)
{
	last = l;
}


void heartRates::setDay(int d)
{
	day = d;
}

void heartRates::setMonth(int m)
{
	month = m;
}

void heartRates::setYear(int y)
{
	year = y;
}

void heartRates::setcuryear(int c)
{
	curyear = c;
}




heartRates::heartRates (string f, string l, int m, int d, int y, int c)
{
	heartRates::setcuryear(c);
	heartRates::setDay(d);
	heartRates::setMonth(m);
	heartRates::setYear(y);
	heartRates::setFirst(f);
	heartRates::setLast(l);
	heartRates::setAge(age);
}

void heartRates::displayInfo()
{
	cout << "Your information is as follows:" << endl;
	cout << getLast() << ", " << getFirst() << endl;
	cout << getMonth() << "/" << getDay() << "/" << getYear() << endl;
	cout << "Current age is: " << getAge() << endl;
	cout << "Maximum heart rate is: " << getMaximumHeartRate() << endl;
	cout << "Target heart range is between: " << lo << " and " << hi << endl;
}



*/




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

class heartRates
{
	string first;
	string last;
	int month;
	int day;
	int year;
	int age;
	int curyear;
	int max;
	int hi;
	int lo;
	int c;
	int y;

public:
    heartRates (string, string, int, int, int, int);
	void setFirst(string);
	void setLast(string);
	void setMonth(int);
	void setDay(int);
	void setYear(int);
	void setcuryear(int);
	void setAge(int);
	void setTargetHeartRate(int);
	void setMaximumHeartRate(int);
	string getFirst();
	string getLast();
	int getMonth();
	int getDay();
	int getYear();
	int getAge();
	int getcuryear();
	int getMaximumHeartRate();
	int getTargetHeartRate(int);
	void displayInfo();

};*/
Hey butterflyze,

Output such as '-858933460' generally implies an uninitialised variable.
You are 'cout'-ing numerous variables that simply haven't been set properly.

setAge() sets the local argument 'age' using two variables 'c' and 'y' that have not been set.
Even if they had been, it would be updating a local (temporary) variable, not the actual class member 'age'.
When you give member function arguments the same name as member variables you have to be sure which one you are referring to. Argument names will override class member names. You have to qualify the member via the 'this' pointer if you want to refer to it in such a case.

setMaximumHeartRate() does the same thing, and is in fact never actually called.
And since setTargetHeartRate() is also never called, neither 'hi' or 'lo' will be set.
Also, a function can return at most one value at a time, thus getTargetHeartRate() will only return the first value.
Either create two different methods or return a struct/class.

Lastly, method calls don't need to be qualified with the class name here, thus instead of calling heartRates::'method'(), you can just call 'method'() directly.

Hope this helps.

Orinoco
Last edited on
Thanks. I think im getting it somewhat. I am only in the beginning of my sencond C++ class at school so forgive me if i take it step by step. I had a feeling that I would have to use pointers somewhere in the function definitions. I've changed it just a bit to separate hi and lo. Also Ive gotten rid of setage(). I need to have the calculation for it in getage() as i have it here:

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
/*#include <iostream>
#include <string>
#include "heartRates.h"
using namespace std;

void heartRates::setFirst(string f)
{
	first = f;
}

void heartRates::setLast(string l)
{
	last = l;
}


void heartRates::setDay(int d)
{
	day = d;
}

void heartRates::setMonth(int m)
{
	month = m;
}

void heartRates::setYear(int y)
{
	year = y;
}

void heartRates::setcuryear(int c)
{
	curyear = c;
}

string heartRates::getFirst()
{
	return first;
}

string heartRates::getLast()
{
	return last;
}

int heartRates::getMonth()
{
	return month;
}

int heartRates::getDay()
{
	return day;
}

int heartRates::getYear()
{
	return year;
}

int heartRates::getcuryear()
{
	return c;
}

int heartRates::getAge(int c, int y)
{
	curage = int(c - y);
	return curage;
}

int heartRates::getMaximumHeartRate()
{
	max = 220 - curage;
	return max;
}

int heartRates::getHiTargetHeartRange(int max)
{
	hi = int(max * .85);
	return hi;
}
int heartRates::getLoTargetHeartRange(int max)
{
	lo = int(max * .5);
	return lo;
}


heartRates::heartRates (string f, string l, int m, int d, int y, int c)
{
	heartRates::setcuryear(c);
	heartRates::setDay(d);
	heartRates::setMonth(m);
	heartRates::setYear(y);
	heartRates::setFirst(f);
	heartRates::setLast(l);
	
}

void heartRates::displayInfo()
{
	cout << "Your information is as follows:" << endl;
	cout << getLast() << ", " << getFirst() << endl;
	cout << getMonth() << "/" << getDay() << "/" << getYear() << endl;
	cout << "Current age is: " << getAge(c, y) << endl;
	cout << "Maximum heart rate is: " << getMaximumHeartRate() << endl;
	cout << "Target heart range is between: " << lo << " and " << hi << endl;
}

*/



ive changed the header to reflect the changes and main is exactly the same. My problem is that am not sure where to implement a pointer??? should it be in the main to specify the value in curage (formerly age) or am I completely lost. And you say that c and y are not set. But am I not initializing c and y in the main(?)
Uhm, wait hold on, forget about pointers for a second. :)

First of all I think I see what the problem is with 'c' and 'y'. You use both those names as arguments which is fine, but you also have members in the class with that name, and in addition you have members called 'curyear' and 'year'. I think you actually don't want the 'c' and 'y' members, and that you are getting confused by what is what as I mentioned earlier.
To make it easier on yourself name all arguments for the funcions; 'name'_in.

Thus:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Class
{
	public:

		void fnSetNAME( type NAME_IN );
		type fnGetNAME( void ) const;

	private:

		type NAME;
};

void Class::fnSetNAME( type NAME_IN )
{
	NAME = NAME_IN;
}

type Class::fnGetNAME( void ) const
{
	return( NAME );
}


where:
type { string, int, etc. }
NAME { Age, Month, etc. }

Once you do that, things will likely become more clear. ;)
Last edited on
Funny how things just click sometimes. I thought I would never get it right!!!! But here it is and it works:
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
/*#include <iostream>
#include <string>
#include "heartRates.h"
using namespace std;

void heartRates::setFirst(string f)
{
	first = f;
}

void heartRates::setLast(string l)
{
	last = l;
}


void heartRates::setDay(int d)
{
	day = d;
}

void heartRates::setMonth(int m)
{
	month = m;
}

void heartRates::setYear(int y)
{
	year = y;
}

void heartRates::setcuryear(int c)
{
	curyear = c;
}

string heartRates::getFirst()
{
	return first;
}

string heartRates::getLast()
{
	return last;
}

int heartRates::getMonth()
{
	return month;
}

int heartRates::getDay()
{
	return day;
}

int heartRates::getYear()
{
	return year;
}

int heartRates::getcuryear()
{
	return c;
}

int heartRates::getAge(int c, int y)
{
	curage = int(curyear - year);
	return curage;
}

int heartRates::getMaximumHeartRate()
{
	max = 220 - curage;
	return max;
}

int heartRates::getHiTargetHeartRange(int max)
{
	hi = int(max * .85);
	return hi;
}
int heartRates::getLoTargetHeartRange(int max)
{
	lo = int(max * .5);
	return lo;
}


heartRates::heartRates (string f, string l, int m, int d, int y, int c)
{
	heartRates::setcuryear(c);
	heartRates::setDay(d);
	heartRates::setMonth(m);
	heartRates::setYear(y);
	heartRates::setFirst(f);
	heartRates::setLast(l);
	
}

void heartRates::displayInfo()
{
	cout << "Your information is as follows:" << endl;
	cout << getLast() << ", " << getFirst() << endl;
	cout << getMonth() << "/" << getDay() << "/" << getYear() << endl;
	cout << "Current age is: " << getAge(c, y) << endl;
	cout << "Maximum heart rate is: " << getMaximumHeartRate() << endl;
	cout << "Target heart range is between: " << getLoTargetHeartRange(max) << " and " << getHiTargetHeartRange(max) << endl;
}
*/



THANKS SOOOOOOO MUCH FOR YOUR PATIENCE!!!!!



I see this has been solved already which is great.

I'm just curious...

why did you comment out all the code when you posted it? I've seen people do this before... I'm just trying to understand what they're thinking.
Maybe they misinterpret this: http://www.cplusplus.com/articles/firedraco1/
@Bazzy
hahaha. that was funny.. ^^
Topic archived. No new replies allowed.