summaryrefslogtreecommitdiffstats
path: root/C++/Ch6Practice1/Ch6Practice1.cpp
blob: 4623714aaaa4e7bf15fbd9a55a5ddeb56a6a24df (plain) (blame)
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
// Name: msglm
// Date: 
// Program Name:
// Description: 


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//Function header (probably should be in a header file)
int civi_to_mili(int startTime, string meridian);
int mili_to_civi(int startTime, string &meridian);

// Named constants

int main() {

	//Variable declaration
	int hour;
	int minute;
	int choice;
	string meridian;
	int trueHour;
	//Program title and description for the user
	cout << "Title: Military-To-Civilian time converter" << endl << "Description: converts military to civilian time and vice-versa" << endl;
	do {

		// User input
		cout << "Enter the hour" << endl;
		cin >> hour;

		cout << "Enter the minute" << endl;
		cin >> minute;
		trueHour = hour;
		
		cout << "AM or PM? (either works if military time)" << endl;
		cin >> meridian;

		if (!cin) {
			cin.clear();
			cin.ignore(100, '\n');
		}

	} while (!cin);

	do {
		cout << "1. Convert Civi to Mili\n";
		cout << "2. Convert Mili to Civi\n";
		cout << "3. Print current time\n";
		cout << "4. Exit\n";
		cout << "Choice: \n";
		cin >> choice;
		if (choice == 1) {
			trueHour = civi_to_mili(trueHour, meridian);
			cout << "\033[2J\033[1;1H";
			cout << "time: \n";
			cout << trueHour << ":" << minute << " " << meridian << endl;
		} else if (choice == 2) {
			trueHour =  mili_to_civi(trueHour, meridian);
			cout << "\033[2J\033[1;1H";
			cout << "time: \n";
			cout << trueHour << ":" << minute << " " << meridian << endl;
		} else if (choice == 3) {
			cout << "\033[2J\033[1;1H";
			cout << "time: \n";
			cout << trueHour << ":" << minute << " " << meridian << endl;
		} else {
			return 0;
		}
	} while (cin);
}

int civi_to_mili(int startTime, string meridian) {
	if (meridian == "PM"){
		return startTime + 12;
	} else {
		return startTime;
	}
}
int mili_to_civi(int startTime, string &meridian) {
	if (startTime > 12){
		meridian = "PM";
		return startTime-12;
	} else {
		meridian = "AM";
		return startTime;
	}
}

/*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */