Hi all,
I have a programing question that reads: Write a functional decomposition and a C++ program that reads a time in numeric form and prints it in English. The time is input as hours and minutes, separated by a space. Hours arespecified in 24-hour time(14 is 2 pm, etc.), but the output should be in 12-hour AM/PM form. Note that noon and midnight are special cases. Examples:
Enter Time: 12 00
Noon
Enter time: 6 44
Six forty four AM
And it says that this program should provide ample opportunity to use Switch Statements.
So, I have used switch statements, plenty of them as you will see in the code, but I am not sure how to do the AM/ PM part. Should I use an while loop for the entire code. I believe my switch statements are correct, but I could be wrong. Also if you spot anything else wrong or weird with my code that would be great if you could let me know. Or should I use void functions to complete, I am somewhat lost. I think I have to use the get function. So I will try that, but I welcome all suggestions.
I am working in C++ with Pico Text editor. Thanks for all of your help, you are a big help. And I have not compiled it yet either because it is not complete.
CODE
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int time;
int minutes;
int hours;
int tens;
cout << "Please enter a time in the 24 hour clock format: " << endl;
cin >> time;
if (time > 0000 && time < 1200)
time = AM;
else
time = PM;
switch (hours)
{
case '1' :
case '13': cout << "One";
break;
case '2' :
case '14': cout << "Two";
break;
case '3' :
case '15': cout << "Three";
break;
case '4' :
case '16': cout << "Four";
break;
case '5' :
case '17': cout << "Five";
break;
case '6' :
case '18': cout << "Six";
break;
case '7' :
case '19': cout << "Seven";
break;
case '8' :
case '20': cout << "Eight";
break;
case '9' :
case '21': cout << "Nine";
break;
case '10':
case '22': cout << "Ten";
break;
case '11':
case '23': cout << "Eleven";
break;
case '12': cout << "Noon";
}
switch(tens)
{
case '20': cout << "twenty";
break;
case '30': cout << "thirty";
break;
case '40': cout << "forty";
break;
case '50': cout << "fifty";
}
switch(minutes)
{
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;
case '13': cout << "thirteen";
break;
case '14': cout << "fourteen";
break;
case '15': cout << "fifthteen";
break;
case '16': cout << "sixteen";
break;
case '17': cout << "seventeen";
break;
case '18': cout << "eighteen";
break;
case '19': cout << "nineteen";
break;
}
cout << hours << " " << tens << " " << minutes << " " << time;
return 0;
}
Note: Tens are the tens of minutes. Should I use subfunctions?
Thanks again.