我如何使它,无论用户用空白字符串按 Enter 多少次,它总是打印"开始"字符串?
How do I make it so no matter how many times the user presses enter with a blank string, it always prints the 'start' string?
这是我的代码 - 如何使此循环不受限制? 像任何终端一样,当您用空白字符串按回车键时,它会重写您的根
#include <iostream>
#include <windows.h>
std::string input_t;
void startup(std::string start) {
std::cout << start;
while(true) {
while ( n = std::cin.get() ) {
if ( int n == (int)'n' ) {
std::cout << start;
std::cin >> input_t;
}
else {
std::cout << "Program is terminating...n";
exit(EXIT_FAILURE);
}
}
}
}
int main() {
startup("User:DESKTOP$ ");
std::cin.get();
}
您可以使用std::getline
继续读取字符串,直到换行符:
while(std::getline(std::cin, input_t))
{
if (line.empty())
{
std::cout << start;
continue;
}
// else do stuff with input_t, or break
}
相关文章:
- 有没有办法将字符串打印为文字?
- 如何通过标准字符串打印 ostream 变量的值?
- 这个随机字符串打印代码有什么问题?
- 比较 C++ 字符串打印中的两个整数数组
- 在C++中从后缀表达式字符串打印单个操作
- 没有得到 ASCII 编号 = 7 的字符串打印输出
- C++字符串到字符串打印不正确,无法获取 unicode 路径
- 如何从数组 c++ 中的字符串打印第 N 个字符
- 将 LPCWSTR 字符串打印到文件
- C++正在将DWORD_PTR*字符串打印到文件中
- 使用#define字符串打印f和警告
- 使用格式字符串打印类
- 如何将字符转换为字符串,并从字符串打印到main函数
- 将vector中结构体的成员字符串打印到cout流
- fprintf() / std::cout 不会将部分字符串打印到 stdout/stderr
- 为什么ostream为定义为' volatile char[] '的字符串打印' 1 ' ?
- c++文件读取和字符串打印
- 如何使用一个字符串打印此unicode行
- 按字符串打印日期
- 如何用c++将字符串打印到控制台