+ 1

Military Time - Help!

I can’t figure out how to satisfy test case 5! It seems like I’ve tried everything. Anything I might be missing? https://code.sololearn.com/c8dHxqq34NU8/?ref=app

29th Jan 2022, 12:58 AM
Sean
8 Antworten
+ 3
Hey Sean .... It's your use of %i. Change them all to %d & it works!
29th Jan 2022, 8:56 AM
HungryTradie
HungryTradie - avatar
+ 3
I messed about with some extra brackets at your if && - || statements, no pass. I played with minutes = 0 to be minutes = 00, no pass. I then tried your code with: *str="12:00 PM"; I got 00:00 An then with *str="12:00 AM"; I got 00:00 Could that be case 5? EDIT: oops, that was something I did to your code. Doesn't do it now.
29th Jan 2022, 3:46 AM
HungryTradie
HungryTradie - avatar
+ 3
I noticed you use digit for malloc, why not 👉digit*sizeof(ptr)👈? I don't bother checking that pointers are not NULL when using them, nor do I bother to free() in code coach. Both of those would be excellent habits. Are you allowing space for the '\0' in your strings/pointers? I had missed learning sscanf, thanks for showing me the easy way to convert strings!!! I had also neglected to learn %02d, seems I'm moving too quickly for the information to stick in my old brain....
29th Jan 2022, 8:48 AM
HungryTradie
HungryTradie - avatar
+ 2
Hello HungryTradie Thanks for helping with my code! I know the condition that “test case 5” demands now, but I’m not sure yet how I will go about fulfilling it. In a nutshell, the program needs to be able to take a time value with a trailing zero as input (08:00 AM). My scanf statement was mishandling this criterion. I’ll post my updated code when I’ve cracked it! Any ideas welcome! I like your modulo implementation to remove error checking and clean things up; I’ll try it out. Thanks!
29th Jan 2022, 8:39 AM
Sean
+ 1
G'day Sean you can link your code bits, but you can't link to the code challenge attempts. Many people copy from their attempt & save it into a new code bit.
29th Jan 2022, 3:34 AM
HungryTradie
HungryTradie - avatar
+ 1
Thanks HungryTradie I fixed it in the original post.
29th Jan 2022, 3:40 AM
Sean
+ 1
G'day Sean Did you get this working? I looked at a few other people's codes, and have then implemented a working version in C. I used your idea of checking for 'P' and then *12. After that I used modulo to bring 24 back to 0. No error checking, no wrong input detection, nothing fancy. Good luck mate! for those reading this in prosperity: https://code.sololearn.com/c6K4C2vphSRj/?ref=app
29th Jan 2022, 8:29 AM
HungryTradie
HungryTradie - avatar
+ 1
You’re right; it worked! Wow, thanks so much for all your help! I reposted my code since I was able to pare it down along the way: https://code.sololearn.com/c8dHxqq34NU8/?ref=app
29th Jan 2022, 4:54 PM
Sean