Greatmar2 Greatmar2 - 2 months ago 12
C Question

C toggling logic function not working as expected

I need a function to read in a schedule (that uses the values in

enum heatingScheduleEnum {off, on };
) and display at what times the schedule has toggled between on and off. I have written a function to do this, but it does not work as expected - working only until the first time that the value is toggled on then off again. After this, it outputs that the schedule is being toggled off each time it is actually marked as being on.

I have worked mainly with Java and C++, but this is in C. The logic looks perfectly fine to me and I cannot see why it does not work.

The interpretTime function takes a time in minutes (0-1439 is a full day) and converts it into a string readable by humans.

My only thought on what could be causing this is the copious size of the array. But the lecturers provided the sample array itself, and they did get the project tested.

Thank you!

Here is the function:

void printHeatingSchedule(heatingScheduleType * schedule) {//Print out times that the heating toggles
char timeStr[5];
int i, isOn, preOn = 0;

for(i = 0; i < 1440; i++) {
isOn = schedule[i];
if (isOn != preOn) {
interpretTime(i, timeStr); //Makes timeStr into a string readable by humans
if(isOn) {
printf("Heating turns on at %s\n", timeStr);
}
else {
printf("Heating turns off at %s\n", timeStr);
}
preOn = isOn;
}
}
}


Here is my interpretTime function:

void interpretTime(const int timeMin, char timeStr[5]) { //Convert a minute value of time into a string readable by humans
int hours, minutes;

hours = timeMin/60; //Integer devision truncates decimals
minutes = timeMin%60; //Remainder will be in minutes
sprintf(timeStr, "%02d:%02d", hours, minutes);
}


Here is the output:

Heating turns off at 03:00
Heating turns off at 03:01
Heating turns off at 03:02
Heating turns off at 03:03
Heating turns off at 03:04
Heating turns off at 03:05
Heating turns off at 03:06
Heating turns off at 03:07
Heating turns off at 03:08
Heating turns off at 03:09
Heating turns off at 03:10
Heating turns off at 03:11
Heating turns off at 03:12
Heating turns off at 03:13
Heating turns off at 03:14
Heating turns off at 03:15
Heating turns off at 03:16
Heating turns off at 03:17
Heating turns off at 03:18
Heating turns off at 03:19
Heating turns off at 03:20
Heating turns off at 03:21
Heating turns off at 03:22
Heating turns off at 03:23
Heating turns off at 03:24
Heating turns off at 03:25
Heating turns off at 03:26
Heating turns off at 03:27
Heating turns off at 03:28
Heating turns off at 03:29
Heating turns off at 03:30
Heating turns off at 03:31
Heating turns off at 03:32
Heating turns off at 03:33
Heating turns off at 03:34
Heating turns off at 03:35
Heating turns off at 03:36
Heating turns off at 03:37
Heating turns off at 03:38
Heating turns off at 03:39
Heating turns off at 03:40
Heating turns off at 03:41
Heating turns off at 03:42
Heating turns off at 03:43
Heating turns off at 03:44
Heating turns off at 03:45
Heating turns off at 03:46
Heating turns off at 03:47
Heating turns off at 03:48
Heating turns off at 03:49
Heating turns off at 03:50
Heating turns off at 03:51
Heating turns off at 03:52
Heating turns off at 03:53
Heating turns off at 03:54
Heating turns off at 03:55
Heating turns off at 03:56
Heating turns off at 03:57
Heating turns off at 03:58
Heating turns off at 03:59
Heating turns off at 04:00
Heating turns off at 04:01
Heating turns off at 04:02
Heating turns off at 04:03
Heating turns off at 04:04
Heating turns off at 04:05
Heating turns off at 04:06
Heating turns off at 04:07
Heating turns off at 04:08
Heating turns off at 04:09
Heating turns off at 04:10
Heating turns off at 04:11
Heating turns off at 04:12
Heating turns off at 04:13
Heating turns off at 04:14
Heating turns off at 04:15
Heating turns off at 04:16
Heating turns off at 04:17
Heating turns off at 04:18
Heating turns off at 04:19
Heating turns off at 04:20
Heating turns off at 04:21
Heating turns off at 04:22
Heating turns off at 04:23
Heating turns off at 04:24
Heating turns off at 04:25
Heating turns off at 04:26
Heating turns off at 04:27
Heating turns off at 04:28
Heating turns off at 04:29
Heating turns off at 04:30
Heating turns off at 04:31
Heating turns off at 04:32
Heating turns off at 04:33
Heating turns off at 04:34
Heating turns off at 04:35
Heating turns off at 04:36
Heating turns off at 04:37
Heating turns off at 04:38
Heating turns off at 04:39
Heating turns off at 04:40
Heating turns off at 04:41
Heating turns off at 04:42
Heating turns off at 04:43
Heating turns off at 04:44
Heating turns off at 04:45
Heating turns off at 04:46
Heating turns off at 04:47
Heating turns off at 04:48
Heating turns off at 04:49
Heating turns off at 04:50
Heating turns off at 04:51
Heating turns off at 04:52
Heating turns off at 04:53
Heating turns off at 04:54
Heating turns off at 04:55
Heating turns off at 04:56
Heating turns off at 04:57
Heating turns off at 04:58
Heating turns off at 04:59
Heating turns off at 05:00
Heating turns off at 05:01
Heating turns off at 05:02
Heating turns off at 05:03
Heating turns off at 05:04
Heating turns off at 05:05
Heating turns off at 05:06
Heating turns off at 05:07
Heating turns off at 05:08
Heating turns off at 05:09
Heating turns off at 05:10
Heating turns off at 05:11
Heating turns off at 05:12
Heating turns off at 05:13
Heating turns off at 05:14
Heating turns off at 05:15
Heating turns off at 05:16
Heating turns off at 05:17
Heating turns off at 05:18
Heating turns off at 05:19
Heating turns off at 05:20
Heating turns off at 05:21
Heating turns off at 05:22
Heating turns off at 05:23
Heating turns off at 05:24
Heating turns off at 05:25
Heating turns off at 05:26
Heating turns off at 05:27
Heating turns off at 05:28
Heating turns off at 05:29
Heating turns off at 05:30
Heating turns off at 16:00
Heating turns off at 16:01
Heating turns off at 16:02
Heating turns off at 16:03
Heating turns off at 16:04
Heating turns off at 16:05
Heating turns off at 16:06
Heating turns off at 16:07
Heating turns off at 16:08
Heating turns off at 16:09
Heating turns off at 16:10
Heating turns off at 16:11
Heating turns off at 16:12
Heating turns off at 16:13
Heating turns off at 16:14
Heating turns off at 16:15
Heating turns off at 16:16
Heating turns off at 16:17
Heating turns off at 16:18
Heating turns off at 16:19
Heating turns off at 16:20
Heating turns off at 16:21
Heating turns off at 16:22
Heating turns off at 16:23
Heating turns off at 16:24
Heating turns off at 16:25
Heating turns off at 16:26
Heating turns off at 16:27
Heating turns off at 16:28
Heating turns off at 16:29
Heating turns off at 16:30
Heating turns off at 16:31
Heating turns off at 16:32
Heating turns off at 16:33
Heating turns off at 16:34
Heating turns off at 16:35
Heating turns off at 16:36
Heating turns off at 16:37
Heating turns off at 16:38
Heating turns off at 16:39
Heating turns off at 16:40
Heating turns off at 16:41
Heating turns off at 16:42
Heating turns off at 16:43
Heating turns off at 16:44
Heating turns off at 16:45
Heating turns off at 16:46
Heating turns off at 16:47
Heating turns off at 16:48
Heating turns off at 16:49
Heating turns off at 16:50
Heating turns off at 16:51
Heating turns off at 16:52
Heating turns off at 16:53
Heating turns off at 16:54
Heating turns off at 16:55
Heating turns off at 16:56
Heating turns off at 16:57
Heating turns off at 16:58
Heating turns off at 16:59
Heating turns off at 17:00
Heating turns off at 17:01
Heating turns off at 17:02
Heating turns off at 17:03
Heating turns off at 17:04
Heating turns off at 17:05
Heating turns off at 17:06
Heating turns off at 17:07
Heating turns off at 17:08
Heating turns off at 17:09
Heating turns off at 17:10
Heating turns off at 17:11
Heating turns off at 17:12
Heating turns off at 17:13
Heating turns off at 17:14
Heating turns off at 17:15
Heating turns off at 17:16
Heating turns off at 17:17
Heating turns off at 17:18
Heating turns off at 17:19
Heating turns off at 17:20
Heating turns off at 17:21
Heating turns off at 17:22
Heating turns off at 17:23
Heating turns off at 17:24
Heating turns off at 17:25
Heating turns off at 17:26
Heating turns off at 17:27
Heating turns off at 17:28
Heating turns off at 17:29
Heating turns off at 17:30
Heating turns off at 17:31
Heating turns off at 17:32
Heating turns off at 17:33
Heating turns off at 17:34
Heating turns off at 17:35
Heating turns off at 17:36
Heating turns off at 17:37
Heating turns off at 17:38
Heating turns off at 17:39
Heating turns off at 17:40
Heating turns off at 17:41
Heating turns off at 17:42
Heating turns off at 17:43
Heating turns off at 17:44
Heating turns off at 17:45
Heating turns off at 17:46
Heating turns off at 17:47
Heating turns off at 17:48
Heating turns off at 17:49
Heating turns off at 17:50
Heating turns off at 17:51
Heating turns off at 17:52
Heating turns off at 17:53
Heating turns off at 17:54
Heating turns off at 17:55
Heating turns off at 17:56
Heating turns off at 17:57
Heating turns off at 17:58
Heating turns off at 17:59
Heating turns off at 18:00
Heating turns off at 18:01
Heating turns off at 18:02
Heating turns off at 18:03
Heating turns off at 18:04
Heating turns off at 18:05
Heating turns off at 18:06
Heating turns off at 18:07
Heating turns off at 18:08
Heating turns off at 18:09
Heating turns off at 18:10
Heating turns off at 18:11
Heating turns off at 18:12
Heating turns off at 18:13
Heating turns off at 18:14
Heating turns off at 18:15
Heating turns off at 18:16
Heating turns off at 18:17
Heating turns off at 18:18
Heating turns off at 18:19
Heating turns off at 18:20
Heating turns off at 18:21
Heating turns off at 18:22
Heating turns off at 18:23
Heating turns off at 18:24
Heating turns off at 18:25
Heating turns off at 18:26
Heating turns off at 18:27
Heating turns off at 18:28
Heating turns off at 18:29
Heating turns off at 22:00
Heating turns off at 22:01
Heating turns off at 22:02
Heating turns off at 22:03
Heating turns off at 22:04
Heating turns off at 22:05
Heating turns off at 22:06
Heating turns off at 22:07
Heating turns off at 22:08
Heating turns off at 22:09
Heating turns off at 22:10
Heating turns off at 22:11
Heating turns off at 22:12
Heating turns off at 22:13
Heating turns off at 22:14
Heating turns off at 22:15
Heating turns off at 22:16
Heating turns off at 22:17
Heating turns off at 22:18
Heating turns off at 22:19
Heating turns off at 22:20
Heating turns off at 22:21
Heating turns off at 22:22
Heating turns off at 22:23
Heating turns off at 22:24
Heating turns off at 22:25
Heating turns off at 22:26
Heating turns off at 22:27
Heating turns off at 22:28
Heating turns off at 22:29
Heating turns off at 22:30
Heating turns off at 22:31
Heating turns off at 22:32
Heating turns off at 22:33
Heating turns off at 22:34
Heating turns off at 22:35
Heating turns off at 22:36
Heating turns off at 22:37
Heating turns off at 22:38
Heating turns off at 22:39
Heating turns off at 22:40
Heating turns off at 22:41
Heating turns off at 22:42
Heating turns off at 22:43
Heating turns off at 22:44
Heating turns off at 22:45
Heating turns off at 22:46
Heating turns off at 22:47
Heating turns off at 22:48
Heating turns off at 22:49
Heating turns off at 22:50
Heating turns off at 22:51
Heating turns off at 22:52
Heating turns off at 22:53
Heating turns off at 22:54
Heating turns off at 22:55
Heating turns off at 22:56
Heating turns off at 22:57
Heating turns off at 22:58
Heating turns off at 22:59
Heating turns off at 23:00
Heating turns off at 23:01
Heating turns off at 23:02
Heating turns off at 23:03
Heating turns off at 23:04
Heating turns off at 23:05
Heating turns off at 23:06
Heating turns off at 23:07
Heating turns off at 23:08
Heating turns off at 23:09
Heating turns off at 23:10
Heating turns off at 23:11
Heating turns off at 23:12
Heating turns off at 23:13
Heating turns off at 23:14
Heating turns off at 23:15
Heating turns off at 23:16
Heating turns off at 23:17
Heating turns off at 23:18
Heating turns off at 23:19
Heating turns off at 23:20
Heating turns off at 23:21
Heating turns off at 23:22
Heating turns off at 23:23
Heating turns off at 23:24
Heating turns off at 23:25
Heating turns off at 23:26
Heating turns off at 23:27
Heating turns off at 23:28
Heating turns off at 23:29
Heating turns off at 23:30
Heating turns off at 23:31
Heating turns off at 23:32
Heating turns off at 23:33
Heating turns off at 23:34
Heating turns off at 23:35
Heating turns off at 23:36
Heating turns off at 23:37
Heating turns off at 23:38
Heating turns off at 23:39
Heating turns off at 23:40
Heating turns off at 23:41
Heating turns off at 23:42
Heating turns off at 23:43
Heating turns off at 23:44
Heating turns off at 23:45
Heating turns off at 23:46
Heating turns off at 23:47
Heating turns off at 23:48
Heating turns off at 23:49
Heating turns off at 23:50
Heating turns off at 23:51
Heating turns off at 23:52
Heating turns off at 23:53
Heating turns off at 23:54


And here is the schedule (provided by the univesity):

#define heatingScheduleTypicalValues {off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , off , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , on , off , off , off , off , off }

Answer

You need to extract the parts of schedule into the right structure, not just int.

Also timestr is not quite long enough so some of your other variables might be being overwritten by interpretTime

Comments