2

In K.N. King's book C Programming: A modern approach, chapter 7 there is an exercise 9 (and also exercise 8), which asks the user to enter a 12-hour time and then converts it into a 24-hour one.

e.g. 9:11 PM -> 21:11

To my question, I understand how to get the times in the format of e.g. 8:37PM or 8:37[space]PM but I have no idea how to make it work for both formats.

Thanks

int h, m;
char meridiem;

printf("Enter a 12-hour time: ");
scanf_s("%d:%d%c", &h, &m, &meridiem);

printf("%c\n", meridiem);

NOTE: I do realise that I am getting only one character.

goyf
  • 43
  • 1
  • 6
  • 2
    In the code you submitted nothing converts anything, even the `meridiem` designation which should be a two-character word in your description, only its first character is getting scanned. Further on, `scanf_s()` doesn't even belong to the C standard, it is a Microsoft extension. – user3078414 May 22 '16 at 10:30
  • If available on your platform you want to look at `strptime()`/`strftime()` (http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html / http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html#) – alk May 22 '16 at 10:35
  • 1
    Also please note `char meridiem;` stores only *one* character. – alk May 22 '16 at 10:41
  • Related (though not a duplicate of this): [Convert 12-hour date/time to 24-hour date/time](https://stackoverflow.com/questions/440061/convert-12-hour-date-time-to-24-hour-date-time/). – Jonathan Leffler May 22 '16 at 11:13
  • 1
    Note that (a variant of) [`scanf_s()`](https://msdn.microsoft.com/en-us/library/w40768et.aspx) is specified in Annex K of ISO/IEC 9899:2011 — the C11 standard. The difference is mainly in the type of the buffer-length arguments — `unsigned` in MS and `rsize_t` in Standard C. See also [Do you use the TR-24731 "safe" functions?](https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions) – Jonathan Leffler May 22 '16 at 11:29

2 Answers2

2

Processing user input is best handled in 2 steps: input, parsing. Then process the data.

Step 1: Simple read the user input

printf("Enter a 12-hour time: ");
fflush(stdout);  // Insure output is completely sent

char buf[80];
if (fgets(buf, sizeof buf, stdin) == NULL) Handle__InputClosed();

Step 2: parse the input. Many approaches. This one uses "%n" to detect the end of a successfully parsed input. "%n" is used to save the current offset of the scan. The space in "%d %1[aApP]" allows for the optional occurrence of space on user input between the minute and AM/PM.

int hour, minute;
char meridian[2];
int n = 0;
sscanf(buf, "%d:%d %1[aApP]%*[mM] %n", &hour, &minute, meridian, &n); 
if (n == 0 || buf[n] || hour < 0 || minute < 0 || hour >= 12 || minute >= 60) {
  puts("Bad input");
  return -1;
} 

Step 3: Process the validated input

meridian[0] = toupper(meridian[0]);
if (meridian[0] == 'A') {
  if (hour >= 12) hour -= 12;  // 12:00 AM --> 0:00
} else {
  hour += 12;
}
printf("%d:%02d\n", hour, minute);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1
#include<stdio.h>

int main(){
    char time[10];
    int hour,minute,second;

    gets(time);

        hour=(time[0]-'0')*10+(time[1]-'0');
        minute=(time[3]-'0')*10+(time[4]-'0');
        second=(time[6]-'0')*10+(time[7]-'0');
        if(hour==12&&time[8]=='A')
            printf("%02d:%02d:%02d\n",hour-12,minute,second);
        else if(hour==12&&time[8]=='P')
            printf("%02d:%02d:%02d\n",hour,minute,second);
        else if(hour<12&&time[8]=='A')
            printf("%02d:%02d:%02d\n",hour,minute,second);
        if(hour<12&&time[8]=='P')
            printf("%02d:%02d:%02d\n",hour+12,minute,second);

    return 0;
} 
Musadul Islam
  • 341
  • 3
  • 11