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 125 126 127 128 129 130 131 132 133
|
#include <iostream> //allows use of cout and endl commands
#include <iomanip> // allows use of manipulation commands (setprecision, ect)
using namespace std; //This line allows you to use cin, cout and endl without the std:: prefix
// function declaration so function can be put after main
void menu();
void convertNormalHours(int, int);
void convertEuropeanHours(int, int);
void displayInfo(int, int);
int getHours(int, int, char);
int main()//heading of the function main, every program must have a main function
{
// variable declaration
int minutesEntered, convertedHours, hoursEntered, choice;
char am;
// sets constant integers for use with menu function
const int GETDATA = 1, CONVERT12 = 2, CONVERT24 = 3, DISPLAYDATA = 4, GETMENU = 5, QUIT = 6;
//Welcome screen
cout << "This program will take in a number between 1 and 24 and convert the time from 24-hour format to the 12-hour time or vice versa. " << endl;
cout << endl;
//start of loop
do
{
menu(); //show menu function
cin >> choice; //variable for selection from menu function
switch (choice) //switch start
{
case GETDATA: //get data switch to gather info for computation
cout << "Please enter the hours you wish converted: "; // data gathering
cin >> hoursEntered; // data gathering
if (hoursEntered < 13) // determines if hours are am or pm
{
cout << "Is this AM? "; // data gathering
cin >> am;// data gathering
}
else
am = 'n'; // if greater than 12 sets to pm
getHours(hoursEntered, convertedHours, am); //I could not figure out how to get convertedHours and am to go into the function if they were not already previously defined
cout << endl << "Please enter the minutes: "; // info gathering
cin >> minutesEntered;// info gathering
break;
case CONVERT12:
convertNormalHours(convertedHours, minutesEntered);// executes function to convert convertedHours to 12 hour time
break;
case CONVERT24:
convertEuropeanHours(convertedHours, minutesEntered);// executes function to convert convertedHours to 24 hour time
break;
case DISPLAYDATA:
displayInfo(hoursEntered, minutesEntered); //displays data entered by user
break;
case GETMENU:
menu(); //displays the menu
break;
}
} while (choice != QUIT); //if 6 is entered quits the program
system("pause"); //pauses the command window to see program running
return 0; //ends the program
}
void convertNormalHours(int convertedHours, int minutesEntered) // sets function type as one that is not returning data to the program, brings in convertedHours and enteredMinutes
{
if (convertedHours == 0)//if statement to deal with 12 AM issue not being calculatable easily
{
cout << endl << "The time is " << "12" << ":" << minutesEntered << " AM" << endl << endl; //time output
}
else if (convertedHours < 12) //else if to get rid of the 0 hours return and output time if AM
{
cout << endl << "The time is " << convertedHours << ":" << minutesEntered << " AM" << endl;
cout << endl;
}
else // output for PM
{
cout << endl << "The time is " << convertedHours << ":" << minutesEntered << " PM" << endl;
cout << endl;
}
}
void convertEuropeanHours(int convertedHours, int minutesEntered)// sets function type as one that is not returning data to the program, brings in convertedHours and enteredMinutes
{
if (convertedHours == 0)//checks if convertedHours is equal to 0 for 24 hour time and outputs the 00 used instead of the single 0
{
cout << endl << "The time is 00" << minutesEntered << endl;
cout << endl;
}
else//outputs 24 hour time format without 0 issue
{
cout << endl << "The time is " << convertedHours << minutesEntered << endl;
cout << endl;
}
}
void menu()// sets function type as one that is not returning data to the program, only displays the menu choices, all text
{
cout << endl;
cout << "Menu Choices: " << endl;
cout << endl;
cout << "Press 1 to enter the hours and minutes you request to store for conversion: " << endl;
cout << "Press 2 to convert to 12 hour time: " << endl;
cout << "Press 3 to convert to 24 hour time: " << endl;
cout << "Press 4 to display the hours and minutes you have stored for conversion: " << endl;
cout << "Press 5 to show this menu: " << endl;
cout << "Press 6 to exit the program: " << endl;
cout << "Please enter a selection: ";
}
void displayInfo (int hoursEntered, int minutesEntered) // sets function type as one that is not returning data to the program, brings in convertedHours and enteredMinutes
{
cout << endl << "You entered the following hours for conversion: " << hoursEntered << " and the following minutes: " << minutesEntered; //outputs data entered by user without conversion
cout << endl;
}
int getHours(int hoursEntered, int convertedHours, char am)
{
if (am == 'y') //verifies if am is set to yes
convertedHours = hoursEntered + 12; // hours outpout when am set to yes
if (convertedHours == 24) // swaps 12 am to 0 for 24 hour time
convertedHours = 0; // swaps 12 am to 0 for 24 hour time
else
convertedHours = hoursEntered;// sets convertedhours equal to the hours entered for non am times
return convertedHours;
}
|