-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.cpp
107 lines (98 loc) · 1.91 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
#include "input.h"
#include "trim.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
void printMainMenu()
{
cout << "Choose the option to be performed:\n"
<< " 1. Create_Tree\n"
<< " 2. Insertion\n"
<< " 3. Deletion\n"
<< " 4. Search\n"
<< " 5. Traversal\n"
<< " 6. Delete_Tree\n"
<< " 7. Check_Balance\n"
<< " 8. Exit\n";
}
int getMainMenuInput()
{
int selection;
bool isBadSelection;
do
{
selection = getIntInput();
isBadSelection = selection < 1 || selection > 8;
if (isBadSelection)
cout << "Bad Selection. Try Again.\n";
} while (isBadSelection);
return selection;
}
void printTraversalMenu()
{
cout << "Choose the traverse type:\n"
<< " 1. Pre-order\n"
<< " 2. Post-order\n"
<< " 3. In-order\n";
}
int getTravMenuInput()
{
int selection;
bool isBadSelection;
do
{
selection = getIntInput();
isBadSelection = selection < 1 || selection > 3;
if (isBadSelection)
cout << "Bad Selection. Try Again.\n";
} while (isBadSelection);
return selection;
}
int getIntInput()
{
string str;
bool badInput = false;
int input;
do
{
do
{
badInput = false;
cout << "> ";
getline(cin, str);
}
while (!stringIsInt(str));
try { input = stoi(str); }
catch(...)
{
cout << "Error Processing Integer. Try Again.\n";
badInput = true;
}
} while (badInput);
return input;
}
bool stringIsInt(string str)
{
trim(str);
bool isInt = false;
if(str.length())
{
isInt = isdigit(str[0]) || str[0] == '-';
for(int i = 1; i < str.length() && isInt; i++)
isInt = isdigit(str[i]) && isInt;
if(!isInt)
cout << "Please Only Enter a Single Integer. Try Again.\n";
}
return isInt;
}
vector<int> getFileInput()
{
ifstream infile("input.txt");
vector<int> arrayOfValues;
int value;
while (infile >> value)
arrayOfValues.push_back(value);
return arrayOfValues;
}