-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cpp
executable file
·125 lines (104 loc) · 2.43 KB
/
Input.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
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include "Input.h"
#include "Seeds.h"
char* Input::getChars(int num, std::string msg) {
char *chars = new char[num];
bool success;
do {
std::cout << msg << num << std::endl;
success = readChars(chars, num);
} while (!success);
return chars;
}
int Input::numRotors() {
int numbers[1];
bool success;
do {
std::cout << "How many rotors should be added to Enigma? (max. 10)?" << std::endl;
success = readNumbers(numbers, 1, 1, 10);
} while (!success);
return numbers[0];
}
RotorType Input::addRotor(int pos) {
int numbers[1];
bool success;
do {
std::cout << "Which rotor type do you want to choose for position " << pos << "?" << std::endl;
std::cout << "[1] ROTOR_TYPE_I\n[2] ROTOR_TYPE_II\n[3] ROTOR_TYPE_III\n[4] ROTOR_TYPE_IV\n[5] ROTOR_TYPE_V" << std::endl;
success = readNumbers(numbers, 1, 1, 5);
} while (!success);
switch (numbers[0]) {
case 1:
return ROTOR_TYPE_I;
break;
case 2:
return ROTOR_TYPE_II;
break;
case 3:
return ROTOR_TYPE_III;
break;
case 4:
return ROTOR_TYPE_IV;
break;
case 5:
return ROTOR_TYPE_V;
break;
default:
return ROTOR_TYPE_I;
break;
}
}
bool Input::readNumbers(int numbers[], int numNumbers, int min, int max) {
std::string input;
std::stringstream inputStream;
int numInputs = 0;
bool error = false;
std::getline(std::cin, input);
inputStream.str(input);
for (int i = 0; i < numNumbers; ++i) {
inputStream >> numbers[i];
if (numbers[i] < min || numbers[i] > max) {
error = true; break;
}
numInputs++;
}
if (numInputs != numNumbers) {
error = true;
}
if (!error) {
return true;
}
inputStream.clear();
std::cout << "\nError: Invalid input!\n";
return false;
}
bool Input::readChars(char chars[], int numChars) {
std::string input;
std::stringstream inputStream;
int numInputs = 0;
bool error = false;
std::getline(std::cin, input);
inputStream.str(input);
for (int i = 0; i < numChars; ++i) {
inputStream >> chars[i];
if (chars[i] < 'A' || chars[i] > 'Z') {
error = true; break;
}
numInputs++;
}
if (numInputs != numChars) {
error = true;
}
if (!error) {
return true;
}
inputStream.clear();
std::cout << "\nError: Invalid input!\n";
return false;
}
bool Input::readString(std::string str) {
std::getline(std::cin, str);
return true;
}