-
Notifications
You must be signed in to change notification settings - Fork 0
/
invalidOption.h
82 lines (73 loc) · 2.61 KB
/
invalidOption.h
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
#include<iostream>
#include <limits>
#include<conio.h>
using namespace std;
void invalidOption(int &userInput){
bool validInput = false;
while (!validInput) {
// cout << "\t\t\t Enter option: ";
cout << "Enter option: ";
if (cin >> userInput) {
// Input is successful
validInput = true;
} else {
// Clear the error flag and ignore the rest of the input
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid input. Please enter an integer." << endl;
}
}
cout << "You entered: " << userInput << endl;
}
void mustBeInteger(int &userInput){
bool validInput = false;
while (!validInput) {
// cout << "\t\t\t Enter option: ";
// cout << "Enter option: ";
if (cin >> userInput) {
// Input is successful
validInput = true;
} else {
// Clear the error flag and ignore the rest of the input
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid input. Please enter an integer: ";
}
}
cout << endl << "You entered: " << userInput << endl;
}
bool isNumber(const string &s) {
return !s.empty() && s.find_first_not_of("0123456789") == string::npos;
}
bool isValidPhoneNumber(const string &number) {
return (number.length() == 10) && isNumber(number);
}
void invoiceInput(long long &userInput){
// -------------------------------------------------------------------
string str = to_string(userInput);
do {
cin >> str;
if (!isValidPhoneNumber(str)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter an 10 digit number of type integer: ";
}
} while (!isValidPhoneNumber(str));
userInput = stoll(str);
cout << endl << "You entered: " << userInput << endl;
}
void pinType(int &userInput){
bool validInput = false;
while (!validInput) {
if ((cin >> userInput) && (to_string(userInput).length() == 4)) {
// Input is successful
validInput = true;
} else {
// Clear the error flag and ignore the rest of the input
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid input. Please enter an 4 digit number of type integer: ";
}
}
cout << endl << "You entered: " << userInput << endl;
}