forked from NBnfork/Group21RPS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidChar.cpp
More file actions
60 lines (54 loc) · 2.02 KB
/
Copy pathvalidChar.cpp
File metadata and controls
60 lines (54 loc) · 2.02 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
/*********************************************************************
* Author: Samantha Tone
* Date: 11/1/2017
* Description: Implementation file for input validation
*********************************************************************/
#include "validChar.hpp"
using std::cout;
using std::cin;
using std::endl;
using std::string;
char validChar(string strIn, char arrIn[], int arrSize) {
bool loopEnder = true;
char validatedChar;
do {
// resets loopEnder to true, so it will flip to false if not
// all conditions are met
loopEnder = true;
// if the strIn.length is not a single char
if (strIn.length() > 1) {
// flip our bool to false to continue loop
loopEnder = false;
// ask user for new input
cout << "String entered is too long. Please try again."
<< endl;
getline(cin, strIn);
} else {
// set this as false before for loop, so we know if it
// gets flipped back to true
loopEnder = false;
for (int i = 0; i < arrSize; i++) {
// if the char in strIn is equal to the char at
// position i of arrIn
if (strIn[0] == arrIn[i]) {
// flips to a true loopEnder bool
loopEnder = true;
// assigns the matching char to our validatedChar
validatedChar = arrIn[i];
}
}
// if the loopEnder is never flipped in the for loop,
// we know that it does fit the available chars
if (loopEnder == false) {
// ask user for new input
cout << "String entered is not one of the specified char options. Please try again."
<< endl;
getline(cin, strIn);
} else {
// breaks out of do/while loop
break;
}
}
} while (loopEnder == false);
return validatedChar;
}