Variables initialized in a class become deinitialized?

I have a class named TimeCard that passes a time and logs it to either punchIn or punchOut. line 58 is able to pass the time variables from TimeCard and set it to the private set in Time2.

But once getCurrentTime is called to retrieve hours/minutes/seconds, the variables are set to random numbers and I am unable to call getCurrentTime()
for example: hours h =8... when getCurrentTime() is called, i get h = -83461.. any help would be greatly appreciated!

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
class Time2 {
private:
	friend class TimeCard;
	string formatted;
	int hour;
	int minute;
	int second;
	bool ampm;

public:
	Time2() {

	}
	//overloaded constructor
	Time2(int hour, int minute, int second, bool ampm) {
		this->hour = hour;
		this->minute = minute;
		this->second = second;
		this->ampm = ampm;
		cout << hour << minute << second; //variables are correct here
	}
	string getCurrentTime() {

		formatted = hour << ':' << minute << ':' << second;
		return formatted;   //hours, minutes, and seconds are random variables, instead of the specified input
}

	Tom[1].clockOut(hours, minutes, seconds, ampm);

			 

	return 0;
};

class TimeCard : public Time2 {
	private:
		int punchInH, punchInM, punchInS, punchOutH, punchOutM, punchOutS;
		const double RATE = 12.50;
		Time2 punchInTime, punchOutTime;
		string WorkerID;
		double payrate;
		bool hasPunched = false;
		double hoursWorked;
		string sTime, eTime;

	public:
		TimeCard() {

		}
		//overloaded constructor
		TimeCard::TimeCard(string WorkerID, int h, int m, int s, bool ap) {

			if (hasPunched == false)
				hasPunched = true;


			this->WorkerID = WorkerID;
			Time2(h, m, s, ap);


			if (hasPunched == true) {
				getPunchInTime();
				punchInH = getHours();
				punchInM = getMinutes();
				punchInS = getSeconds();
			}
			else {
				//executes if user has punched out. Logs hours appropriotely.
				getPunchOutTime();
				punchOutH = getHours();
				punchOutM = getMinutes();
				punchOutS = getSeconds();
				hoursWorked = getHoursWorked();
			}

		
		};


		
		//sets time to string
		void getPunchInTime() {

			sTime = getCurrentTime();

		}
		//sets time to string
		void getPunchOutTime() {
			eTime = getCurrentTime();
		}

		
	
};



int main()
{
        //variables initialized...                                                                        
	TimeCard Tom [1] = { TimeCard(iD, hours, minutes, seconds, ampm) };

        //variables set differently
	Tom[1].clockOut(hours, minutes, seconds, ampm);

			 

	return 0;
Last edited on
If this
TimeCard Tom [1]
is an attempt to make an array of one TimeCard object, then the way to access the first (and only) object in that array is as follows:
Tom[0]

The first element in an array is element 0, not element 1.


It's a bit hard to answer your question because this code doesn't compile. It's got lots of problems.

Last edited on
Lines 28-33: Appears to be a code fragment from someplace else. These lines don't belong here.

Line 24: You can't use << with a string. Use a stringstream if you want to use <<.

Lines 63-65, 70-73: Where are these functions?

Line 101,104: Where are variables: iD, hours, minutes, seconds, ampm

Line 101,104: If you're only defining a single occurrence, forget the [1].

Topic archived. No new replies allowed.