-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputValidation.cpp
242 lines (204 loc) · 5.57 KB
/
inputValidation.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***************************************************************************************************
** Author: Michael Johnson
** Date: 7/19/2017
** Description: Definitions of Input Validation functions
***************************************************************************************************/
#include "inputValidation.h"
#include <iostream>
#include <string>
using std::stoi;
using std::string;
using std::cout;
using std::cin;
using std::endl;
/**************************************************************************************
**
**************************************************************************************/
InputValid::InputValid()
{
string input = "";
}
/**************************************************************************************
**
**************************************************************************************/
int InputValid::isPosInteger()
{
int testInt; // Variable to hold an integer converted from a string
tries = 0; // Variable to hold the number of times a user has tried to enter
// a value
do
{
counter = 0; // Increments is character is not a digit or is less than 0
// If the user has tried to enter a positive integer more than once it prompts
// a message
if (tries > 0)
{
cout << "Please enter a positive integer" << endl;
getline(cin, input);
while (input == "");
{
do
{
cout << "Please enter a positive integer" << endl;
getline(cin, input);
} while (input == "");
}
}
// Captures line on first try
else
{
getline(cin, input);
if (input == "")
{
do
{
cout << "Please enter a positive integer" << endl;
getline(cin, input);
} while (input == "");
}
}
// Loops through the length of the string and
for (size_t count = 0; count < input.length(); count++)
{
// If the character is not a digit
if (!(isdigit(input[count])))
{
// It increments the count variable
counter++;
}
}
if (counter == 0)
{
// Stores an integer version of the string into testInt
testInt = stoi(input);
if (testInt < 0)
{
counter++;
}
}
tries++;
}while (counter > 0);
return testInt;
}
/**************************************************************************************
**
**************************************************************************************/
string InputValid::isString()
{
tries = 0; // Variable to hold the number of times a user has tried to enter
// a value
do
{
counter = 0; // Increments is character is not a digit or is less than 0
// If the user has tried to enter a string more than once it prompts
// a message
if (tries > 0)
{
cout << "Please enter a string" << endl;
getline(cin, input);
}
// Captures line on first try
else
{
getline(cin, input);
}
// Loops through each character of string
for (size_t count = 0; count < input.length(); count++)
{
// If the character is a digit
if (isdigit(input[count]))
{
// Then it increments the counter
counter++;
}
}
// If none of the characters in the string were digits
/*if (counter == 0)
{
// Loop through string and turn each character into lowercase
for (size_t count = 0; count < input.length(); count++)
{
input[count] = tolower(input[count]);
}
}*/
// Increment tries at end of loop if it fails so the next loop
// will ask the user to enter a string
tries++;
} while (counter > 0);
return input;
}
/**************************************************************************************
**
**************************************************************************************/
double InputValid::isFloat()
{
tries = 0; // Variable to hold the number of times a user has tried to enter
// a value
do
{
counter = 0; // Increments is character is a digit or is less than 0
if (tries > 0) // If the user has tried to enter a float more than once it prompts
// a message
{
cout << "Please enter a Float" << endl;
getline(cin, input);
}
// Captures line on first try
else
{
getline(cin, input);
}
for (size_t count = 0; count < input.length(); count++)
{
// If the element is not a digit
if (!(isdigit(input[count])))
{
// and it is a period
if (input[count] == '.')
{
counter++;
}
// If it's something other than a period, the counter increments
// by 2 so it will exit the do/while loop and retry
else
{
counter += 2;
}
}
}
// If the counter equals 1, then all elements were digits except 1 period
if (counter == 1)
{
return stod(input);
}
// If it's anything other than 1, then tries will increment
else if (counter != 1)
{
tries++;
}
} while (counter > -1);
}
/**************************************************************************************
**
**************************************************************************************/
char InputValid::isChar()
{
tries = 0; // Variable to hold the number of times a user has tried to enter
// a value
do
{
if (tries > 0) // If the user has tried to enter a character more than once it prompts
// a message
{
cout << "Please enter a character" << endl;
getline(cin, input);
}
// Captures line on first try
else
{
getline(cin, input);
}
tries++;
} while (input.length() > 1 || !isalpha(input[0]));
return input[0];
}