24hrs Time Converter for Effy~


/ Published in: C++
Save to your folder(s)

dok rok cetong boh


Copy this code and paste it in your HTML
  1. // Effy-20111021.cpp : Defines the entry point for the console application.
  2. // This program written using Visual C++ 2010 bish!!
  3.  
  4. // printf(); Reference: http://www.cplusplus.com/reference/clibrary/cstdio/printf/
  5. // scanf(); Reference: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
  6. // Modulo splitting digits: http://www.daniweb.com/software-development/c/threads/310740
  7. /*********************************************************************************
  8. d1 = r % 10 * Thinking from right to left, we first store d1 *
  9. r /= 10 * Now we drop the last digit of r, as we don't need it anymore *
  10.  
  11. * By the time we've got all of d1, d10 and d100, r will be equal to r / 1000 *
  12. **********************************************************************************/
  13.  
  14. //#include <stdio.h> // Include standard libs instead of stdafx.h (the windows shit)
  15. #include "stdafx.h" // Windows precompiled library and shits.
  16. #include <iostream> // Include standard C++ Libs.
  17. //#include <iomanip> // Reference on setw + setfill http://msdn.microsoft.com/en-us/library/daeb650d(VS.80).aspx
  18.  
  19. using namespace std;
  20.  
  21. int _tmain()
  22. {
  23. // We gonna use some variables.
  24. int usertime = 0;
  25. int hours = 0;
  26. int minutes = 0;
  27.  
  28. printf ("Enter time in 24hrs format: ");
  29. //scanf_s ("%d", &usertime);
  30. cin >> usertime;
  31.  
  32. //newline setting
  33. printf("\n\n");
  34.  
  35. // Splitting minutes and hours using modulo operation.
  36. minutes = usertime % 100;
  37. hours = usertime / 100;
  38.  
  39. // Validating Minutes first. (There's workaround for it actually)
  40. if (minutes >= 0 && minutes < 60) {
  41.  
  42. // 12 AM Flag
  43. if (hours == 0 || hours == 24){
  44. printf ("24hrs time is: %d.\n", usertime);
  45. printf ("12hrs time is: 12:%02d AM.\n", minutes);
  46.  
  47. // 1 AM to 11AM
  48. } else if (hours >= 1 && hours <= 11) {
  49. printf ("24hrs time is: %04d.\n", usertime);
  50. printf ("12hrs time is: %02d:%02d AM.\n", hours, minutes);
  51.  
  52. // 12PM to 11PM (On-fly conversion. woohoo
  53. } else if (hours >= 12 && hours <= 23) {
  54. printf ("24hrs time is: %04d.\n", usertime);
  55. printf ("12hrs time is: %02d:%02d PM.\n", hours%12 , minutes);
  56. } else {
  57. printf ("Invalid time!");
  58. }
  59. } else {
  60. printf ("Please reconsider your minute value!\n\n");
  61. }
  62.  
  63. system("Pause");
  64. return 0;
  65. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.