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
|
#ifndef USER_INPUT_HANDLER_H
#define USER_INPUT_HANDLER_H
// file : #include "user_input_handler.h"
#include <string>
class UserInputHandler
{
public:
char GetSingleCharacter();
int GetInteger(const int& min, const int& max);
double GetDouble();
bool GetBoolean();
std::string GetString(int charnumber = -1, bool empty = true);
};
#endif /* USER_INPUT_HANDLER_H */
// file: user_input_handler.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;
char UserInputHandler::GetSingleCharacter()
{
string str;
char letter;
int i;
const string invalid_char_input_("Failure: Too many characters.");
const string char_not_allowed_("Failure: Invalid character.");
bool loopend = false;
while (loopend == false) {
std::getline(cin, str);
i = (int)str.length();
if (string::npos == str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
if (i > 1) {
cout << invalid_char_input_ << endl;
} else {
letter = str[0];
cout << "Character Entered: " << letter << endl;
loopend = true;
}
} else {
cout << char_not_allowed_ << endl;
}
}
return letter;
}
int UserInputHandler::GetInteger(const int& min, const int& max)
{
const string invalid_int_("Failure: No integer found.");
const string low_int_("Failure: Integer is below minimum value.");
const string high_int_("Failure: Integer is above maximum value.");
int result;
bool loopend = false;
while (loopend == false) {
string line;
if (getline(cin, line)) {
stringstream ss(line);
if (ss >> result && ss.eof()) {
if (result < min) {
cout << low_int_ << endl;
} else if (result > max) {
cout << high_int_ << endl;
} else {
cout << "Integer Entered is: " << result << endl;
loopend = true;
}
} else {
cout <<invalid_int_ << endl;
result = 0;
}
}
}
return result;
}
double UserInputHandler::GetDouble()
{
const string invalid_double_("Failure: No double found.");
double result;
string line;
bool loopend = false;
while (loopend == false) {
if (getline(cin, line)) {
stringstream ss(line);
if (!(ss >> result && ss.eof())) {
cout <<invalid_double_ << endl;
} else {
cout << "Double Entered is: " << result << endl;
loopend = true;
}
}
}
return result;
}
bool UserInputHandler::GetBoolean()
{
string str;
bool test;
string result;
const string invalid_bool_("Failure: Must input 'T', 'F', 'true', or 'false'. Case sensitive.");
bool loopend = false;
while (loopend == false) {
std::getline(cin, str);
if (str == "T") {
test = true;
result = "true";
loopend = true;
} else if ( str == "F") {
test = false;
result = "false";
loopend = true;
} else if (str == "true") {
test = true;
result = "true";
loopend = true;
} else if (str == "false") {
test = false;
result = "false";
loopend = true;
} else {
cout << invalid_bool_ << endl;
}
}
cout << "Boolean: " << result << endl;
return test;
}
string UserInputHandler::GetString(int charnumber, bool empty)
{
const string too_long_string_("Failure: Too many characters");
const string empty_string_("Failure: String is empty.");
int i;
bool infinite;
if (charnumber == -1) {
infinite = true;
}
string str;
bool loopend = false;
while (loopend == false) {
std::getline(cin, str);
i = (int)str.length();
if (i == 0) {
if (empty == false) {
cout << empty_string_ << endl;
} else {
cout << "String Entered: " << str << endl;
loopend = true;
}
} else if (i > charnumber) {
if (infinite == false) {
cout << too_long_string_ << endl;
} else {
cout << "String Entered: " << str << endl;
loopend = true;
}
} else {
cout << "String Entered: " << str << endl;
loopend = true;
}
}
return str;
}
int main()
{
UserInputHandler handler;
handler.GetSingleCharacter();
handler.GetInteger(0,100);
handler.GetDouble();
handler.GetBoolean();
handler.GetString();
}
|