-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsInputValidate.h
122 lines (107 loc) · 2.62 KB
/
clsInputValidate.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
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
#pragma once
#include <iostream>
#include "clsDate.h"
using namespace std;
class clsInputValidate
{
public:
static void Srand()
{
//Seeds the random number generator in C++, called only once
srand((unsigned)time(NULL));
}
static int RandomNumber(int From, int To)
{
//Function to generate a random number
int randNum = rand() % (To - From + 1) + From;
return randNum;
}
static bool IsNumberBetween(int Number, int From, int To)
{
return (Number >= From && Number <= To);
}
static bool IsNumberBetween(double Number, double From, double To)
{
return (Number >= From && Number <= To);
}
static bool IsDateBetween(clsDate DateCheck, clsDate Date1, clsDate Date2)
{
if (
(clsDate::IsDate1EqualsDate2(DateCheck, Date1) || clsDate::IsDate1AfterDate2(DateCheck, Date1))
&&
(clsDate::IsDate1EqualsDate2(DateCheck, Date2) || clsDate::IsDate1LessThanDate2(DateCheck, Date2))
)
return true;
if (
(clsDate::IsDate1EqualsDate2(DateCheck, Date1) || clsDate::IsDate1LessThanDate2(DateCheck, Date1))
&&
(clsDate::IsDate1EqualsDate2(DateCheck, Date2) || clsDate::IsDate1AfterDate2(DateCheck, Date2))
)
return true;
}
static int ReadIntNumber(string ErrorMessage)
{
int Number = 0;
cin >> Number;
while (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << ErrorMessage << endl;
cin >> Number;
}
return Number;
}
static double ReadDoubleNumber(string ErrorMessage)
{
double Number = 0;
cin >> Number;
while (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << ErrorMessage << endl;
cin >> Number;
}
return Number;
}
static float ReadFloatNumber(string ErrorMessage)
{
float Number = 0;
cin >> Number;
while (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << ErrorMessage << endl;
cin >> Number;
}
return Number;
}
static int ReadIntNumberBetween(int From, int To, string ErrorMessage)
{
int Number = ReadIntNumber("Error number, try again");
while (! IsNumberBetween(Number,From,To))
{
cout << ErrorMessage << endl;
Number = ReadIntNumber("Error number, try again");
}
return Number;
}
static double ReadDoubleNumberBetween(double From, double To, string ErrorMessage)
{
double Number = ReadDoubleNumber("Error number, try again");
while (! IsNumberBetween(Number,From,To))
{
cout << ErrorMessage << endl;
Number = ReadDoubleNumber("Error number, try again");
}
return Number;
}
static string ReadString()
{
string s;
getline(cin >> ws, s);
return s;
}
};