-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestManager.cpp
More file actions
55 lines (50 loc) · 1.61 KB
/
Copy pathRequestManager.cpp
File metadata and controls
55 lines (50 loc) · 1.61 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
#include "RequestManager.h"
#include <iostream>
namespace ClearKeyboard {
void ClearInputBuffer() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
using namespace ClearKeyboard;
int RequestManager::GetValidatedUserInput(int user_input) {
while (!(std::cin >> user_input)) {
std::cout << "Invalid type. Please enter a number.\n";
ClearInputBuffer();
}
ClearInputBuffer();
return user_input;
}
struct NodePositionInfo RequestManager::InsertRequest(struct Node* current_node) {
std::cout << "\nADD NODE: \n---------\n";
std::cout << "Enter a number: ";
node.data = GetValidatedUserInput(node.data);
if (current_node != nullptr) {
int total_nodes = list_info.GetNumberOfNodes(current_node);
std::cout << "Enter a position between 1 and " << total_nodes + 1 << " to insert node: ";
node.position = GetValidatedUserInput(node.position);
}
return node;
}
struct NodePositionInfo RequestManager::DeleteRequest(struct Node* current_node) {
std::cout << "\nDELETE NODE: \n------------\n";
int total_nodes = list_info.GetNumberOfNodes(current_node);
switch (total_nodes) {
case 0: {
// List is empty.
break;
}
case 1: {
// Case: Only one node available. Prompt user to enter 1.
std::cout << "Please enter the number 1: ";
node.position = GetValidatedUserInput(node.position);
break;
}
default: {
std::cout << "Enter a position between 1 and " << total_nodes << " to delete node: ";
node.position = GetValidatedUserInput(node.position);
break;
}
}
return node;
}