-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputValidation.cpp
136 lines (113 loc) · 3.25 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
/***************************************************************************************************
** Author: Michael Johnson
** Date: 7/16/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);
}
// Captures line on first try
else
{
getline(cin, 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;
}