我如何使它,无论用户用空白字符串按 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?

本文关键字:字符串 打印 开始 多少次 Enter 用户 空白 何使它      更新时间:2023-10-16

这是我的代码 - 如何使此循环不受限制? 像任何终端一样,当您用空白字符串按回车键时,它会重写您的根

#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
}