Converting from standard time to military time

Write a C++ program that reads a time from the keyboard. The time should be in the format "HH:MM AM" or "HH:MM PM". Hours can be one or two digits, minutes are exactly two digits, and AM/PM can be any mixture of uppercase and lowercase letters. For example, 1:10 am, 12:05 PM, and 12:45 aM are valid inputs. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four-digit military time based on a 24-hour clock. For example, 1:10 AM should output 0110 hours, 11:30 PM would output 2330 hours, 12:15 AM would output 0015 hours and 5:05 Pm would output 1705 hours. The function should return a string to be written to the screen by the main function. I am having trouble making these functions, not sure where to start.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <cstdlib>
 #include <iostream>
 #include <string>
 
 std::string convertToMilitaryTime(std::string);
 
 int main(int argc, char* argv[])
 {
     std::cout << "Enter in a time: ";
     return EXIT_SUCCESS;
 }
 
 std::string convertToMilitaryTime(std::string st)
 {
     return "";
 }
Hello dstuck15,

Welcome to the forum.

Do not think about the whole program right now. First work on getting the input from the user. Then work on breaking up the string into hours, minutes and am/pm.

Once you have this part the other functions will be easier to work with.

When you work in small steps it is much easier.

You can start with what you have posted just add some variables and get some input.

Hope that helps,

Andy

P.S. I was interrupted and forgot to mention if you do not need "argc" "argv" in your program then they do not have to be there.
Last edited on
Topic archived. No new replies allowed.