1

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?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Maz Diaz
  • 37
  • 5

2 Answers2

1

There are multiple problems:

  • The format %[^\n]s is incorrect. There is no s, you should specify the maximum number of characters as 2 and use %2s or %2[^\n], and the destination array should be passed as time or &time[0], but not &time.

  • You should store the number of successful conversions and test that at least 2 numbers have been read and whether the am/pm indicator was present.

  • if (time == "am" ... is incorrect: you cannot compare strings this way. Use the strcmp() function that returns a negative value if the first argument comes before the second in lexicographical order, 0 if they are the same string, and a positive value otherwhise:

      if (!strcmp(time, "am") ...
    

    !strcmp(time, "am") is the same as strcmp(time, "am") == 0

Here is a modified version:

#include <stdio.h>
#include <string.h>

int main() {
    int hour, minute;
    char time[3];
    int n = scanf("%02d:%02d %2s", &hour, &minute, time);

    if (n < 2) {
        printf("invalid format\n");
    } else {
        if (n == 3) {
            if (!strcmp(time, "am")) {
                if (hour >= 12) {
                    hour -= 12;
                }
            } else
            if (!strcmp(time, "pm")) {
                if (hour < 12) {
                    hour += 12;
                }
            } else {
                printf("invalid indicator: %s\n", time);
            }
        }
        printf("%02d:%02d\n", hour, minute);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

I think the problem here is that you are checking an if time is == "am". First thing I would do is use a string but never mind you used an array of chars. Second thing how do you check if an array is == with a string. You must do a for loop to check if the time is equal to "am" or "pm". Or you can do:

if(time[0]=='a' && time[1]=='m'){
      ....
}

Here you've used 2 if-statements but you don't need 2. Because if the time[...] is not equal to "am" then logically it is "pm", but if you don't trust the user then you must do while loop until the user inputs "am" or "pm". Hope this helps.

Skerdi Velo
  • 121
  • 2
  • 13
  • 2
    A `char` array _is_ a string, specifically a c-string (nothing else exists unless you make it, but you lose the stdlib functions for handling strings if you do). You compare them via `strcmp`. – Rogue Jul 13 '22 at 13:33