Answer to Question #42700 in Programming & Computer Science for sherlynn

Question #42700
Design an algorithm that will prompt for and receive the time expressed in 2400 format (e.g. 2305 hours), convert it to 12 hour format (eg 11.05 p.m.) and display the new time to the screen. Your program is to repeat the processing until a sentinel time of 9999 is entered. (include Input data:
Expected results: Desk check table:)
1
Expert's answer
2014-05-23T10:34:41-0400
#include<iostream>
#include<string>

using namespacestd;

intmain() {
string time24;
string time12;
int hour;
string minutes;
string period;
const string sentinel = "9999";
cout << "Pleaseenter the hour and minutes in 24hr format (ex. 0101 1234 2311)" <<endl;
cin >>time24;

while (time24!= sentinel) {
hour =stoi(time24.substr(0, 2));

if (hour> 11) {
if (hour> 12) {
hour =hour - 12;
}
period = "p.m.";
} else {
if (hour== 0) {
hour =hour + 12;
}
period = "a.m.";
}

minutes =time24.substr(2, 2);
time12 =to_string(hour) + "." +minutes + " " +period;
cout <<time12 << endl;
cout << "Pleaseenter the hour and minutes in 24hr format (ex. 0101 1234 2311)" <<endl;
cin >>time24;
}

return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS