C++ Programming: Converting Time Between 12/24 Hours

I decided to stick my code in a place that I would keep track of it all. I had to write this program to demonstrate the use of functions. While it might not be the most intuitive way of accomplishing the task, I think it will do just fine.

/* * * * * * * * * * * * * * * * * * * * * * 
 * This program will convert a user inputted time between 12 and 24 hours
 * based on the selection.  The user will be able to use the program until
 * they quit.  This program will demonstrate the use of multiple fucntions
 * to complete the task.
 * * * * * * * * * * * * * * * * * * * * * * */

#include <iostream>
#include <string>

//Used to convert time to/from 12/24 hour.
//o is option, h is hour, m is minute, p is period
std::string convertTime( int o, int h, int m, char p ) {
  
  //default statement for time
  std::string time = "\nThe time is: ";

  //declared for AM or PM
  std::string period;

  //if the option is 1, convert hour to 24 hours
  if ( o == 1 ) {
    if ( p == 'p') {
      h += 12;
    }
  }
  
  //if the option is 2, convert hours to 12 hours
  //if the time is greater than 12, then subtract 12 from hour
  if ( o == 2 ) {
    period = "AM";
    if ( h > 12 ) {
      h -= 12;
      period = "PM";
    }
  }

  //convert time to strings
  std::string hour = std::to_string(h);
  std::string minute;

  // if minutes are less than 10, add the leading zero
  if ( m < 10 ) {
    minute = std::to_string(m);
    minute = '0' + minute;
  } else {
    minute = std::to_string(m);
  }

  //concactinate the time
  time += hour + ":" + minute;
  
  //add the period to the end of the time
  if ( o == 2 ) {
    time += " " + period;
  }

  //return the time
  return time;
}

//used to store the menu option
void menu() {
  std::cout << "Options -- " << std::endl;
  std::cout << "1: To convert time from 12-hour notation to 24-hour notation." << std::endl;
  std::cout << "2: To convert time from 24-hour notation to 12-hour notation." << std::endl;
  std::cout << "0: To quit the program." << std::endl;
  std::cout << "Choose: ";
}

//main program
int main() {
  //declared variables for user input
  int option, hour, minutes;
  char period;

  //continuous loop until user chooses to quit
  do {
    //display the menu
    menu();

    //queue user for option from the menu
    std::cin >> option;

    //if an invalid option is chosen, tell user to choose valid option
    if ( (option < 1 || option > 2) && option != 0 ) {
      std::cout << "\nNot a valid option, please choose a valid option. \n" << std::endl;
      menu();
      std::cin >> option;
    }

    //if user chooses to quit
    if (option == 0 ) {

      //end the program with success
      return 0;
    }

    //ask user for hours and minutes
    if ( option == 1 ) {
      do {
      std::cout << "\nEnter hours: ";
      std::cin >> hour;

      //check if hours are valid entry
      if ( hour > 12 ) {
        std::cout << "Not a valid entry, minutes must be less than or equal to 12.";
      }

      } while ( hour > 12 );
    }

    //ask user for hours and minutes
    if ( option == 2 ) {
      do {
      std::cout << "\nEnter hours: ";
      std::cin >> hour;

      //check if hours are valid entry
      if ( hour > 24 ) {
        std::cout << "Not a valid entry, minutes must be less than or equal to 24.";
      }

      } while ( hour > 24 );
    }

  do {
    std::cout << "Enter minutes: "; 
    std::cin >> minutes;

    //check if minutes are valid entry
    if ( minutes > 59 ) {
      std::cout << "Not a valid entry, minutes must be less than 60.\n";
    }

    } while ( minutes > 59 );

    //if user wants to convert 12 to 24 hour, ask for period
    if ( option == 1 ) {
      std::cout << "Enter AM/PM (A or P): ";
      std::cin >> period;

      //if invalid option is chosen, ask user for valid option
      while ( period != 'a' && period != 'p' ) {
        std::cout << "\nInvalid period of day, please choose A or P: ";
        std::cin >> period;
      }

      //forces period to lowercase
      period = std::tolower(period);
    }

    //displays the time based on the convertTime function
    //passes option, hour, minutes and period variables to 
    //convertTime input variables 'o', 'h', 'm' and 'p'
    std::cout << convertTime(option,hour,minutes,period) << "\n\n";

  } while ( option > 0 );

  //end program with success
  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *