-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
51 lines (40 loc) · 1.39 KB
/
utils.h
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
//
// Created by hussein on 8/1/22.
//
#ifndef EXPEDIA_COM_UTILS_H
#define EXPEDIA_COM_UTILS_H
#include<chrono> // for date and time src : https://stackoverflow.com/questions/17223096/outputting-date-and-time-in-c-using-stdchrono
#include<iomanip>
#include<cassert>
using namespace std;
// some utilities for the project
int ReadInt(int min, int max, bool cancel_choice_allowed = false) {
if (!cancel_choice_allowed)
cout << "\nEnter number in range " << min << " - " << max << ": ";
else
cout << "\nEnter -1 to cancel or number in range " << min << " - " << max << ": ";
int val;
cin >> val;
if (cancel_choice_allowed && val == -1)
return val;
if (min <= val && val <= max)
return val;
cout << "ERROR: invalid number...Try again\n";
return ReadInt(min, max);
}
int ShowReadMenu(const vector<string> &choices, string header = "Menu") {
cout << "\n"<<header<<":\n";
for (int ch = 0; ch < (int) choices.size(); ++ch) {
cout << "\t" << ch + 1 << ": " << choices[ch] << "\n";
}
return ReadInt(1, choices.size());
}
// get current time and date
string GetCurrentTimeDate() {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
return ss.str();
}
#endif //EXPEDIA_COM_UTILS_H