- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
Latest commit
133 lines (118 loc) · 5.4 KB
/
Copy pathexample.cpp
File metadata and controls
133 lines (118 loc) · 5.4 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
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
// getinputcpp is a header file that is used for simple input validation
#include<iostream>
#include<sstream>
#include"./getinputcpp.hpp"
voidprint_main_menu();
voidclear_input_example();
voidinteractive_clear_input_example();
voidget_input_example();
voidinteractive_get_input_example();
intmain() {
int choice = 0;
bool keep_going = true;
while (keep_going) {
print_main_menu();
std::cin >> choice;
clear_input(std::cin);
switch (choice) {
case1:
clear_input_example();
break;
case2:
interactive_clear_input_example();
break;
case3:
get_input_example();
break;
case4:
interactive_get_input_example();
break;
case5:
keep_going = false;
break;
}
}
return0;
}
voidprint_main_menu() {
std::cout << "\n+----------------------| Main Menu |-----------------------+\n"
<< "|----------------------------------------------------------|\n"
<< "| 1. clear_input example |\n"
<< "| 2. interactive clear_input example |\n"
<< "| 3. get_input example |\n"
<< "| 4. interactive get_input |\n"
<< "| 5. quit |\n"
<< "+----------------------------------------------------------+\n"
<< "Select an option: ";
}
voidclear_input_example() {
std::cout << "\n+----------------| Clear Input Example |-------------------+\n"
<< "| Initializing variable user_int for storing user input |\n"
<< "| Simulating user input of \"12\\n\" |\n";
int user_int;
std::istringstream input("12\n");
input >> user_int;
std::cout << "| user_int is: " << user_int
<< " after reading into the variable. |\n"
<< "| This leaves the \\n in the input buffer which we can see |\n"
<< "| using peek(): " << input.peek() << ", which is the ASCII character for LF. |\n"
<< "| LF is the control character for newline on Linux. |\n";
clear_input(input);
if (input.peek() == EOF) {
std::cout << "| After calling clear_input on the input buffer, it now |\n"
<< "| reads EOF or end of file. |\n"
<< "+----------------------------------------------------------+\n";
}
}
voidinteractive_clear_input_example() {
std::cout << "\n+-----------| Interactive Clear Input Example|-------------+\n"
<< "|----------------------------------------------------------|\n"
<< "| Your input is going to get stored as an int in user_int. |\n"
<< "| Try entering whatever you like, -1 is to exit. |\n"
<< "| Enter some additional text after the -1 to see how the |\n"
<< "| clear_input function clears the buffer. |\n"
<< "+----------------------------------------------------------+\n"
<< "Enter: ";
int user_int;
const std::string prompt = "Try entering anything you like (-1 to exit): ";
std::cin >> user_int;
while (std::cin.fail() || user_int != -1) {
clear_input(std::cin);
std::cout << prompt;
std::cin >> user_int;
}
std::string remainder = "";
getline(std::cin, remainder);
if (remainder.size() > 38) {
remainder = remainder.substr(0, 38) + "...";
}
std::cout << "You entered -1 and"
<< (remainder.size() != 0 ? remainder : "\\n") << "\n"
<< "Calling clear_input removes everything after the -1 and\n"
<< "leaves a clear buffer for the next cin, getline, or\n"
<< "get_input call\n";
}
voidget_input_example() {
std::cout << "\n+------------------| Get Input Example |------------------+\n"
<< "|----------------------------------------------------------|\n"
<< "| Initializing user_double for storing input and |\n"
<< "| simulating user input of \"1.35\\n\". |\n";
double user_double;
std::istringstream input("1.35\n");
user_double = get_input<double>(input, "");
std::cout << "| The value in user_double is: " << user_double;
if (input.peek() == EOF) {
std::cout << " and there's nothing |\n"
<< "| left in the input buffer |\n"
<< "+----------------------------------------------------------+\n";
}
}
voidinteractive_get_input_example() {
std::cout << "\n+-------------| Interactive Get Input Example |------------+\n"
<< "| Your input is going to be stored as a double |\n"
<< "| Try entering whatever you like, get_input has basic |\n"
<< "| validation built in to make sure the correct type of |\n"
<< "| input is acquired, it also leaves nothing in the buffer. |\n"
<< "+-----------------------------------------------------------+\n";
double user_double = get_input<double>(std::cin, "Enter a double: ");
}