#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;}
Comments