-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppsl.h
More file actions
54 lines (40 loc) · 1.36 KB
/
cppsl.h
File metadata and controls
54 lines (40 loc) · 1.36 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
// CppSimplifyLibrary - Beginner-friendly C++ I/O library
// Licensed under the Apache License, Version 2.0
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
#ifndef CPPSL_H
#define CPPSL_H
#include <iostream>
#include <string>
#include <limits>
namespace csl {
inline std::string InputStringOnNumError =
"Invalid input! Please check your numeric input!";
// Newline print
template<typename T>
inline void outln(const T& x) {
std::cout << x << std::endl;
}
// Print (no NL)
template<typename T>
inline void out(const T& x) {
std::cout << x;
}
// Overload for strings (full line input)
inline void input(const std::string& prompt, std::string& var) {
std::cout << prompt;
std::getline(std::cin, var);
}
// Template for numeric types (int, double, float, long, etc) and other operator>> types, use an overload to accomplish same name in num + str.
template <typename T>
inline void input(const std::string& prompt, T& var) {
std::cout << prompt;
while (!(std::cin >> var)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
std::cout << InputStringOnNumError;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Remove leftover NL
}
}
#endif