I wanted to make some kind of a clock where it changes the hour depending of the time of the day(am or pm). I use the following code:
#include <stdio.h>
int main() {
int hour, minute;
char time[3];
scanf("%02d:%02d %[^\n]s", &hour, &minute, &time);
getchar();
if (time == "am" && hour >= 12) {
hour - 12;
} else {
hour = hour;
}
if (time == "pm" && hour < 12) {
hour + 12;
} else {
hour = hour;
}
printf("%02d:%02d %s\n", hour, minute, time);
return 0;
}
If I input 13:00
and the time is am
, it should change the hour to 01:00
, but it still outputs 13:00
even though its not pm
. I think the problem is in the if
statement, but I don't know what it is exactly. How do I fix this?