+ 1

Can any one tell me what's wrong? This program is of military time.

#include<stdio.h> #include<string.h> #include<time.h> int main() { int h, m; char a[3]; scanf("%d", &h); scanf("%d", &m); scanf(" %s",a); if(((strcmp(a,"AM") == 0) || (strcmp(a,"am") == 0))&& (h == 12)) { h =0; printf("%02d:%02d", h, m); } else if((strcmp (a,"PM")==0)&&((h!=0)&&(h!=12))) { h=h+12; printf("%02d:%02d",h,m); } }

6th Jan 2020, 12:54 PM
Vishwesh Patel
Vishwesh Patel - avatar
2 odpowiedzi
+ 9
Vishwesh Patel you nees to take user input in given format XX:XX and you didn't nees to check for lower case input you also don't need to use two print statements just reduce that here is what I decoded from your code #include<stdio.h> #include<string.h> #include<time.h> int main() { int h, m; char a[3]; scanf("%d:%d %s", &h, &m, a) ; if(((strcmp(a,"AM") == 0)&& (h == 12))) h =0; else if((strcmp (a,"PM")==0)&&((h!=0)&&(h!=12))) h=h+12; printf("%02d:%02d",h,m); }
6th Jan 2020, 1:33 PM
GAWEN STEASY
GAWEN STEASY - avatar
0
Thank you
6th Jan 2020, 2:35 PM
Vishwesh Patel
Vishwesh Patel - avatar