-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidInt.cpp
More file actions
60 lines (50 loc) · 1.33 KB
/
Copy pathvalidInt.cpp
File metadata and controls
60 lines (50 loc) · 1.33 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
/***************************************
Program Name: Project 1
Author: Robert Elsom
Date: 1/10/2019
Description: Checks that inputted integer from the menu function
fits in the parameters of the program. If
it does, it returns the integer that was tested.
**************************************/
#include <string>
#include <iostream>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int validInt( string prompt, int lowerLimit, int upperLimit) {
int testInteger;
bool validFlag = false;
bool isNumber = true;
string choice;
while(validFlag == false) {
cout << prompt;
cin >> choice;
//changes flag isNumber if any part of input is not a digit
for (unsigned int i = 0; i <choice.length(); i++) {
if (!isdigit(choice[i])) {
isNumber = false;
}
}
if(!isNumber) {
cout<< "Sorry, that is not a valid input" << endl;
isNumber= true;
}
//if variable choice passes as being a number, now verify that it is in range and
//fit in the limits of the program parameters
else {
testInteger = stoi(choice);
if (testInteger > upperLimit) {
cout << "Sorry, that number is too large" << endl;
}
else if (testInteger < lowerLimit) {
cout << "Sorry, that number is too small"<< endl;
}
else{
validFlag = true;
}
}
}
return testInteger;
}