-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidationUtility.cpp
199 lines (181 loc) · 5.5 KB
/
validationUtility.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
/************************************************************************
** Program Name: Fantasy Combat Game *
** Author: Hillary Dreikorn *
** Date: 11/12/2017 *
** Description: This is the implementation file for the validation *
** that will be used in the fantasy combat game. *
***********************************************************************/
#include "validationUtility.hpp"
#include <iostream>
#include <string>
#include <cstdlib>
/************************************************************************
* isRightType() *
* This function will take user input and validate for right type then *
* return the value once correct type is entered. For this program the *
* function is validating for integers. *
************************************************************************/
int isRightType()
{
std::string userInput;
int returnInput = 0;
bool validation = true;
while (validation)
{
getline(std::cin, userInput);
for (int i = 0; i < userInput.length(); i++)
{
if (!isdigit(userInput[i]))
{
validation = true;
}
else
{
validation = false;
}
}
if (validation)
{
std::cout << "Only numbers! Try again: ";
}
}
//convert string to int
returnInput = std::atoi(userInput.c_str());
//return validated input to function called from
return returnInput;
}
/************************************************************************
* posNumOnly() *
* This function will take user input and validate for right type then *
* it will validate for input to be positive numbers only. Then it will *
* return the value back to the function it was called from. *
************************************************************************/
int posNumOnly()
{
int posNum;
bool validate = true;
//calling isRightType to make sure correct data type first
posNum = isRightType();
//loop to make sure number is positive too
while (validate)
{
if (posNum < 1)
{
std::cout << "Invalid choice! Try again.\n";
posNum = isRightType();
}
else
{
validate = false;
}
}
return posNum;
}
/************************************************************************
* only1to5() *
* This function will take user input and validate for right type then *
* it will validate for input to 1 to 5 only. Then it will *
* return the value back to the function it was called from. *
************************************************************************/
int only1to5()
{
int num;
bool validate = true;
//calling isRightType to make sure correct data type first
num = isRightType();
//loop to make sure number is positive too
while (validate)
{
if (num < 1 || num >5)
{
std::cout << "Only options are 1, 2, 3, 4, or 5! Try again.\n";
num = isRightType();
}
else
{
validate = false;
}
}
return num;
}
/************************************************************************
* only1to4() *
* This function will take user input and validate for right type then *
* it will validate for input to 1 to 4 only. Then it will *
* return the value back to the function it was called from. *
************************************************************************/
int only1to4()
{
int num;
bool validate = true;
//calling isRightType to make sure correct data type first
num = isRightType();
//loop to make sure number is positive too
while (validate)
{
if (num < 1 || num >4)
{
std::cout << "Only options are 1, 2, 3, or 4! Try again.\n";
num = isRightType();
}
else
{
validate = false;
}
}
return num;
}
/************************************************************************
* only1to3() *
* This function will take user input and validate for right type then *
* it will validate for input to 1 to 3 only. Then it will *
* return the value back to the function it was called from. *
************************************************************************/
int only1to3()
{
int num;
bool validate = true;
//calling isRightType to make sure correct data type first
num = isRightType();
//loop to make sure number is positive too
while (validate)
{
if (num < 1 || num >3)
{
std::cout << "Only options are 1, 2 or 3! Try again.\n";
num = isRightType();
}
else
{
validate = false;
}
}
return num;
}
/************************************************************************
* only1to2() *
* This function will take user input and validate for right type then *
* it will validate for input to 1 to 2 only. Then it will *
* return the value back to the function it was called from. *
************************************************************************/
int only1to2()
{
int num;
bool validate = true;
//calling isRightType to make sure correct data type first
num = isRightType();
//loop to make sure number is positive too
while (validate)
{
if (num < 1 || num >2)
{
std::cout << "Only options are 1 or 2! Try again.\n";
num = isRightType();
}
else
{
validate = false;
}
}
return num;
}