Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
input.h
Go to the documentation of this file.
1
8
9/*
10Copyright (c) 2026 MrXie1109
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files (the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29*/
30
31#pragma once
32#include <cfloat>
33#include <cstdint>
34#include <iostream>
35#include <limits>
36#include <string>
37
38#include "../text/strpp.h"
39
40namespace console {
47 template <class CharT = char>
49 std::basic_ostream<CharT> &os;
50 std::basic_istream<CharT> &is;
51 };
52
55
58
60 std::cout, std::cin};
61
63 std::wcout, std::wcin};
64
74 template <class T = std::string, class CharT = char>
75 T input(const CharT *prompt = "",
77 T tmp;
78 while (true) {
79 is.os << prompt << std::flush;
80 is.is >> tmp;
81 if (!is.is) {
82 is.is.clear();
83 is.is.ignore(
84 std::numeric_limits<std::streamsize>::max(), CharT('\n'));
85 is.os << "Stream Error!" << std::endl;
86 continue;
87 }
88 is.is.ignore(
89 std::numeric_limits<std::streamsize>::max(), CharT('\n'));
90 return tmp;
91 }
92 }
93
101 template <class T = std::string>
102 T w_input(const wchar_t *prompt = L"",
103 const WInputSettings &is = w_input_settings) {
104 T tmp;
105 while (true) {
106 is.os << prompt << std::flush;
107 is.is >> tmp;
108 if (!is.is) {
109 is.is.clear();
110 is.is.ignore(
111 std::numeric_limits<std::streamsize>::max(), wchar_t('\n'));
112 is.os << "Stream Error!" << std::endl;
113 continue;
114 }
115 is.is.ignore(
116 std::numeric_limits<std::streamsize>::max(), wchar_t('\n'));
117 return tmp;
118 }
119 }
120
127 inline long double input_number(const char *prompt = "Type a number: ",
128 const InputSettings &is = input_settings) {
129 return input<long double>(prompt, is);
130 }
131
138 template <class CharT = char>
139 std::basic_string<CharT> input_line(const CharT *prompt = "",
141 std::basic_string<CharT> tmp;
142 is.os << prompt << std::flush;
143 if (is.is.peek() == CharT('\n')) is.is.get();
144 std::getline(is.is, tmp);
145 return tmp;
146 }
147
159 template <class T, class CharT = char>
161 T max,
162 const CharT *prompt = "",
164 T tmp;
165 while (true) {
166 tmp = input<T>(prompt, is);
167 if (tmp < min) {
168 is.os << "less than the minimum value of " << min << std::endl;
169 continue;
170 }
171 if (tmp > max) {
172 is.os << "Greater than the maximum value of " << max
173 << std::endl;
174 continue;
175 }
176 return tmp;
177 }
178 }
179
187 template <class CharT = char>
188 CharT input_char(const CharT *prompt = "",
190 is.os << prompt << std::flush;
191 CharT tmp = static_cast<CharT>(is.is.get());
192 is.is.ignore(std::numeric_limits<std::streamsize>::max(), CharT('\n'));
193 return tmp;
194 }
195
202 inline bool input_yes_or_no(const char *prompt = "Type yes or no: ",
203 const InputSettings &is = input_settings) {
204 while (true) {
205 char tmp = input_char(prompt, is);
206 if (tmp == 'Y' || tmp == 'y')
207 return true;
208 else if (tmp == 'N' || tmp == 'n')
209 return false;
210 else
211 is.os << "Please type yes or no." << std::endl;
212 }
213 }
214
221 template <class CharT = char>
222 std::basic_string<CharT> input_all(const CharT *prompt = "",
224 is.os << prompt;
225 return {std::istreambuf_iterator<CharT>(is.is),
226 std::istreambuf_iterator<CharT>()};
227 }
228}
本库所有组件所在的顶层命名空间。
BasicInputSettings< wchar_t > WInputSettings
BasicInputSettings<wchar_t> 的类型别名。
Definition input.h:57
static BasicInputSettings< wchar_t > w_input_settings
全局默认宽字符输入设置
Definition input.h:62
bool input_yes_or_no(const char *prompt="Type yes or no: ", const InputSettings &is=input_settings)
读取一个 y/n 确认,返回布尔值。
Definition input.h:202
T input(const CharT *prompt="", const BasicInputSettings< CharT > &is=input_settings)
从标准输入读取一个值,支持类型模板。
Definition input.h:75
std::basic_string< CharT > input_all(const CharT *prompt="", const BasicInputSettings< CharT > &is=input_settings)
读取输入流中剩余的全部内容(直到 EOF)。
Definition input.h:222
T min(const MultiArray< T, Dims... > &a)
求最小值。
Definition multiarray.h:1676
T max(const MultiArray< T, Dims... > &a)
求最大值。
Definition multiarray.h:1685
@ T
Definition kb.h:99
static BasicInputSettings input_settings
全局默认输入设置
Definition input.h:59
T w_input(const wchar_t *prompt=L"", const WInputSettings &is=w_input_settings)
读取一行宽字符字符串。
Definition input.h:102
T input_with_range(T min, T max, const CharT *prompt="", const BasicInputSettings< CharT > &is=input_settings)
读取一个在指定范围内的数字。
Definition input.h:160
long double input_number(const char *prompt="Type a number: ", const InputSettings &is=input_settings)
读取一个 long double 类型的数字。
Definition input.h:127
CharT input_char(const CharT *prompt="", const BasicInputSettings< CharT > &is=input_settings)
读取一个字符。
Definition input.h:188
BasicInputSettings< char > InputSettings
BasicInputSettings<char> 的类型别名。
Definition input.h:54
std::basic_string< CharT > input_line(const CharT *prompt="", const BasicInputSettings< CharT > &is=input_settings)
读取一整行字符串(包含空格)。
Definition input.h:139
提供字符串处理工具函数和格式化类。
输入/输出流设置,用于自定义 input 函数的输入输出目标。
Definition input.h:48
std::basic_ostream< char > & os
Definition input.h:49
std::basic_istream< char > & is
Definition input.h:50