Console Library 4.7.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 <iostream>
33#include <string>
34#include <limits>
35#include <cfloat>
36#include <cstdint>
37#include "strpp.h"
38
39namespace console
40{
47 {
48 std::ostream &os;
49 std::istream &is;
50 } inputSettings{std::cout, std::cin};
51
60 template <class T = std::string>
61 T input(const std::string &prompt = "",
62 const InputSettings &is = inputSettings)
63 {
64 T tmp;
65 std::string message;
66 while (true)
67 {
68 is.os << prompt << std::flush;
69 is.is >> tmp;
70 if (!is.is)
71 {
72 is.is.clear();
73 is.is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
74 is.os << "StreamError!\n";
75 continue;
76 }
77 is.is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
78 return tmp;
79 }
80 }
81
88 long double inputNumber(const std::string &prompt = "Type a number: ",
89 const InputSettings &is = inputSettings)
90 {
91 return input<long double>(prompt, is);
92 }
93
100 std::string inputLine(const std::string &prompt = "Type a line string: ",
101 const InputSettings &is = inputSettings)
102 {
103 std::string tmp;
104 is.os << prompt << std::flush;
105 if (is.is.peek() == '\n')
106 is.is.get();
107 std::getline(is.is, tmp);
108 return tmp;
109 }
110
120 long double inputWithRange(const std::string &prompt = "Type a number: ",
121 long double min = DBL_MIN,
122 long double max = DBL_MAX,
123 const InputSettings &is = inputSettings)
124 {
125 long double tmp;
126 while (true)
127 {
128 tmp = input<long double>(prompt, is);
129 if (tmp < min)
130 {
131 is.os << "less than the minimum value of " << min << std::endl;
132 continue;
133 }
134 if (tmp > max)
135 {
136 is.os << "Greater than the maximum value of "
137 << max << std::endl;
138 continue;
139 }
140 return tmp;
141 }
142 }
143
151 char inputChar(const std::string &prompt = "Type a character: ",
152 const InputSettings &is = inputSettings)
153 {
154 is.os << prompt << std::flush;
155 char tmp = is.is.get();
156 is.is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
157 return tmp;
158 }
159
167 bool inputYesOrNo(const std::string &prompt = "Type yes or no: ",
168 const InputSettings &is = inputSettings)
169 {
170 std::string tmp;
171 while (true)
172 {
173 char tmp = inputChar(prompt, is);
174 if (tmp == 'Y' || tmp == 'y')
175 return true;
176 else if (tmp == 'N' || tmp == 'n')
177 return false;
178 else
179 is.os << "Please type yes or no." << std::endl;
180 }
181 }
182
190 std::string inputAll(const std::string &prompt = "",
191 const InputSettings &is = inputSettings)
192 {
193 is.os << prompt;
194 return {std::istreambuf_iterator<char>(is.is),
195 std::istreambuf_iterator<char>()};
196 }
197}
T min(const MultiArray< T, Dims... > &a)
求最小值。
Definition multiarray.h:1551
T max(const MultiArray< T, Dims... > &a)
求最大值。
Definition multiarray.h:1561
本库所有组件所在的顶层命名空间。
std::string inputAll(const std::string &prompt="", const InputSettings &is=inputSettings)
读取输入流中剩余的全部内容(直到 EOF)。
Definition input.h:190
T input(const std::string &prompt="", const InputSettings &is=inputSettings)
从标准输入读取一个值,支持类型模板。
Definition input.h:61
@ T
Definition kb.h:72
long double inputWithRange(const std::string &prompt="Type a number: ", long double min=DBL_MIN, long double max=DBL_MAX, const InputSettings &is=inputSettings)
读取一个在指定范围内的数字。
Definition input.h:120
bool inputYesOrNo(const std::string &prompt="Type yes or no: ", const InputSettings &is=inputSettings)
读取一个 y/n 确认,返回布尔值。
Definition input.h:167
std::string inputLine(const std::string &prompt="Type a line string: ", const InputSettings &is=inputSettings)
读取一整行字符串(包含空格)。
Definition input.h:100
long double inputNumber(const std::string &prompt="Type a number: ", const InputSettings &is=inputSettings)
读取一个 long double 类型的数字。
Definition input.h:88
char inputChar(const std::string &prompt="Type a character: ", const InputSettings &is=inputSettings)
读取一个字符(忽略前导空白,但不跳过换行符?实际使用 get())。
Definition input.h:151
提供字符串处理工具函数和格式化类。
输入/输出流设置,用于自定义 input 函数的输入输出目标。
Definition input.h:47
std::ostream & os
输出提示信息的流
Definition input.h:48
std::istream & is
读取输入的流
Definition input.h:49