Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that inputs a time from the console. The time should be in the f

ID: 3705224 • Letter: W

Question

Write a program that inputs a time from the console. The time should be in the format "HH:MM AM" or "HH:MM PM". Hours may be one or two digits, for example, "1:10 AM or "11:30 PM". 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" would output "0110 hours", "11:30 PM" would output "2330 hours", and 12:15 AM" would output "0015 hours". The function may either write the time to the console or return a string to be written to the console by the main function.

Explanation / Answer

#include<iostream>

#include<string>

//using stringstearm to separate strings separated by ':'

#include<sstream>

using namespace std;

int convert_military_time(string time);

int isStrDigit(string token);

int main()

{

string time;

cout << "Please enter time in format <HH::MM AM/PM>: ";

getline(cin, time);

convert_military_time(time);

}

int convert_military_time(string time)

{

string token[3];

int j = 0;

string military_time;

for (int i = 0; i < time.length(); i++)

{

if (time[i] == ':' || time[i]==32 || time[i]==' ')

{

j++;

}

else

{

token[j] += time[i];

}

}

//now check if first two tokens are numbers

int t[2]; //array to hold hour and min

int ret;

//there are j tokens

int k = 0;

for (int i = 0; i < j; i++)

{

ret = isStrDigit(token[i]);

if (ret == 0) //token is integer

{

t[k++] = stoi(token[i]);

}

if (ret == -2)

{

cout << "token length is more than required length for token, pls check input" << endl;

return -1;

}

}

if (token[2] == "AM")

{

if (t[0] == 12) //12 AM , becomes 00

{

military_time += "00";

military_time += token[1]; //2nd string n token contains MM

military_time += " hours";

}

else

{

if (token[0].length() < 2)

{

military_time += '0';

}

military_time += token[0];

military_time += token[1]; //2nd string n token contains MM

military_time += " hours";

}

}

if (token[2] == "PM")

{

if (t[0] <=11 )

{

military_time += to_string(t[0] + 12);

military_time += token[1]; //2nd string n token contains MM

military_time += " hours";

}

else

{

military_time += token[0];

military_time += token[1]; //2nd string n token contains MM

military_time += " hours";

}

}

cout << "Military time: " << military_time << endl;

return 0;

}

int isStrDigit(string token)

{

if (token.length() > 2) //check length if token should be 2 either for HH , MM or AM or PM

return -2;

for (int i = 0; i < token.length(); i++)

{

if (!isdigit(token[i]))

return -1;

}

return 0;

}

/*output1

Please enter time in format <HH::MM AM/PM>: 1:10 AM

Military time: 0110 hours

//output2

Please enter time in format <HH::MM AM/PM>: 11:30 PM

Military time: 2330 hours

//output3

Please enter time in format <HH::MM AM/PM>: 12:15 AM

Military time: 0015 hours

//output4

Please enter time in format <HH::MM AM/PM>: 1:10 PM

Military time: 1310 hours

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote