-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
103 lines (100 loc) · 2.87 KB
/
Copy pathUtils.cpp
File metadata and controls
103 lines (100 loc) · 2.87 KB
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
/*
==================================================
Mile Stone 2
==================================================
Name : Sangwoo Shin
ID : 119294213
Email : sshin36@myseneca,ca
Section: NCC
Date : 2022/3/15
*/
// I have done all the coding by myself and only copied the code that my professor provided to complete my workshops and assignments.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ctime>
#include "Utils.h"
using namespace std;
namespace sdds {
Utils ut;
void Utils::testMode(bool testmode) {
m_testMode = testmode;
}
void Utils::getSystemDate(int* year, int* mon, int* day) {
if (m_testMode) {
if (day) *day = sdds_testDay;
if (mon) *mon = sdds_testMon;
if (year) *year = sdds_testYear;
}
else {
time_t t = std::time(NULL);
tm lt = *localtime(&t);
if (day) *day = lt.tm_mday;
if (mon) *mon = lt.tm_mon + 1;
if (year) *year = lt.tm_year + 1900;
}
}
int Utils::daysOfMon(int month, int year)const {
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, -1 };
int mon = (month >= 1 && month <= 12 ? month : 13) - 1;
return days[mon] + int((mon == 1) * ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
int Utils::getInt(const char* prompt)
{
int input = 0;
char buffer;
bool valid = false;
if (prompt)
{
cout << prompt;
do
{
cin >> input;
if (cin.fail())
{
cout << "Invalid Integer, retry: ";
cin.clear();
cin.ignore(1000, '\n');
}
else
{
cin.get(buffer);
if (buffer != '\n')
{
cout << "Enter only an integer, try again: ";
cin.clear();
cin.ignore(1000, '\n');
}
else valid = true;
}
} while (!valid);
}
return input;
}
int Utils::getInt(int min, int max, const char* prompt,
const char* errMes)
{
int input = 0;
bool valid = false;
input = getInt(prompt);
do
{
if (input < min || input > max)
{
if (errMes)
{
cout << errMes << ", retry: ";
}
else
{
cout << "Value out of range [" << min << "<=val<=" << max << "]: ";
}
cin >> input;
}
else
{
valid = true;
}
} while (!valid);
return input;
}
}