My (partial) solution to Project 5, written in class.
#include <iostream>
using namespace std;
int main()
{
int hour, minute;
cout << "Enter hour (0-23): ";
cin >> hour;
cout << "Enter minute (0-59): ";
cin >> minute;
// TODO: error-check
bool pm = hour >= 12; // true=PM, false=AM
cout << (pm? "PM" : "AM"); // parens required!
cout << endl;
// TODO: convert to AM/PM
int hour12;
if(hour > 12) {
hour12 = hour - 12;
}
else {
hour12 = hour;
}
if(minute >= 8 && minute <= 22) {
cout << "quarter past ";
}
else if(minute >= 23 && minute <= 37) {
cout << "half past ";
}
else if(minute >= 38 && minute <= 52) {
cout << "quarter til ";
}
if(minute >= 38) {
hour = (hour+1)%24;
hour12++;
}
if(hour == 12) {
cout << "noon";
}
else if(hour == 0) {
cout << "midnight";
}
else {
switch(hour12) {
case 13:
case 1: cout << "one"; break;
case 2: cout << "two"; break;
case 3: cout << "three"; break;
case 4: cout << "four"; break;
case 5: cout << "five"; break;
case 6: cout << "six"; break;
case 7: cout << "seven"; break;
case 8: cout << "eight"; break;
case 9: cout << "nine"; break;
case 10: cout << "ten"; break;
case 11: cout << "eleven"; break;
case 12: cout << "twelve"; break;
}
cout << " in the ";
if(!pm) {
cout << "morning";
}
else if(hour < 18) {
cout << "afternoon";
}
else {
cout << "evening";
}
}
return 0;
}