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
|
void menu(int& ans);
void userInput(int& hours, int& minutes, string& amOrPm, int& ans);
void convertTimeTo24(int& hours, int& minutes);
void convertTimeTo12(int& hours, int& minutes);
void displayResults(int&hours, int&minutes, string& amOrPm, int& ans);
void menu(int& ans) //Function to display the choices
{
cout << "************\n";
cout << "Press 1 to convert time from 24 hour format to 12 hour format\n";
cout << "Press 2 to convert time from 12 hour format to 24 hour format\n";
cout << "Press 00 to terminate the program";
cout << "\n************\n";
cin >> ans;//gets the users choice of what they want to do in the program
}
void userInput(int& hours, int& minutes, string& amOrPm, int& ans)//Function that asks the user for input
{
if (ans == 1)
{
cout << "Please enter the amount of hours: ";
cin >> hours;
cout << "Now enter the amount of minutes: ";
cin >> minutes;
cout << "Do you want it in AM or PM. \n";
cin >> amOrPm; // User just writes AM or PM here
}
else if (ans == 2)
{
cout << "Please enter the amount of hours: ";
cin >> hours;
cout << "Now enter the amount of minutes: ";
cin >> minutes;
}
else if (ans == 0)
{
cout << "You have selected to terminate the program";
}
}
void convertTimeTo24(int& hours, int& minutes)//converts time from 12 hour notation to 24 hour notation
{
if (hours < 12) //hours can't be more than 24 so must make sure it's below 12 before adding 12
{
hours = hours + 12;
}
else // if hours is more than 12 no change will be made
{
hours = hours;
}
}
void convertTimeTo12(int& hours, int& minutes) //converts time from 24 hour notation to 12 hour notation
{
if (hours > 12)//makes sure hours is more than 12 since we can't have negative hours
{
hours = hours - 12;
}
else // if hours is less than 12 it will leave as is
{
hours = hours;
}
}
void displayResults(int&hours, int&minutes, string& amOrPm, int& ans)//function that displays the results
{
if (ans == 1)
cout << "The time is: " << hours << ":" << minutes << " " << amOrPm << endl;
else
cout << "The time is: " << hours << ":" << minutes << endl;
}
int main()
{
int ans, hours, minutes;
string amOrPm;
menu(ans);//pulls up the menu
userInput(hours, minutes, amOrPm, ans); //pulls up the user input function
if (ans == 1)
{
convertTimeTo12(hours, minutes);
}
else if (ans == 2)
{
convertTimeTo24(hours, minutes);
}
displayResults(hours, minutes, amOrPm, ans);
system("pause");
return 0;
}
|