1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <iostream>
using namespace std;
void output();
void time(int& choice);
void input(int& hours, int& minutes, char& type, int& choice, int& hours1);
void output12(int& hours, int minutes, char type, int& choice);
void output24(int& choice, int minutes, int& hours1);
int main()
{
int hours, hours1;
int minutes;
char type;
char answer;
int choice;
do {
output();
time(choice);
input(hours, minutes, type, choice, hours1);
output12(hours, minutes, type, choice);
output24(choice, minutes, hours1);
cout << "\n\nPerform another calculation? (y/n): ";
cin >> answer;
} while ((answer == 'Y') || (answer == 'y'));
return 0;
}
void output()
{
cout << "\nTime Conversion" << endl;
}
void time(int& choice)
{
cout << "Enter the time format you want converted ";
cout << "\nEx... 12 = 8:15 P.M to 20:15";
cout << "\nex... 24 = 20:15 to 8:15 P.M.";
cout << "\n(12 or 24): ";
cin >> choice;
}
void input(int& hours, int& minutes, char& type, int& choice, int& hours1)
{
if (choice == 12)
{
cout << "Enter the hours for the 12 hour time: ";
cin >> hours;
cout << "Enter the minutes for the 12 hour time: ";
cin >> minutes;
cout << "Enter the type of time (A for A.M. or P for P.M.):";
cin >> type;
}
else if (choice == 24)
{
cout << "Enter the hours for the 24 hour time: ";
cin >> hours1;
cout << "Enter the minutes for the 24 hour time: ";
cin >> minutes;
}
else {
cout << "*** An error was made in your input ***" << endl;
}
}
void output12(int& hours, int minutes, char type, int& choice)
{
if (choice == 12)
{
int mil = 0;
if (hours < 12 && hours != 0 && (type == 'A' || type == 'a'))
{
mil = hours;
}
else if (hours < 12 && hours != 0)
{
mil = hours + 12;
}
else if (hours = 12 && (type == 'A' || type == 'a')) {
mil = 0;
}
else {
mil = 12;
}
cout << "\The time converted to 24 hour format is: " << mil << ":" << minutes;
}
}
void output24(int& choice, int minutes, int& hours1)
{
if (choice == 24)
{
int norStd = 0;
if (hours1 <= 12 && hours1 != 0)
{
norStd = hours1;
}
else if (hours1 > 12 && hours1 < 24)
{
norStd = hours1 - 12;
}
else if (hours1 == 0)
{
norStd = 12;
}
else
{
cout << "Incorrect number entered";
}
cout << "The time converted to 12 hour format is: " << norStd << ":" << minutes << " ";
if (hours1 < 12) {
cout << "A.M.";
}
else {
cout << "P.M.";
}
}
}
|