C++,cin,直到使用while循环在线上不再有输入为止
C++, cin until no more input on line using a while loop
我的C++程序有问题。我重新格式化并显示用户在控制台中输入的单词。如果用户输入:嗨,我是bob。在控制台中输入bob后,用户将按enter键。我将重新格式化并以新格式重新打印。问题是,在输入控制台行上的所有单词之前,我不想显示更多输入的消息。我的当前循环要么在每个单词后面显示输入请求,要么根本不显示。这取决于我是否包含提示。我需要让while循环处理每个单词,并输出它,在最后一个单词后停止。什么是布尔参数?我将包含我的代码以供参考。
int _tmain(int argc, _TCHAR* argv[])
{
int b;
string input;
string output ;
int check = 1;
while (check){
cout << "Enter in one or more words to be output in ROT13: " << endl;
cin >> input;
while(my issue is here){
const char *word = input.c_str();
for (int i = 0; i < input.length(); i++){
b = (int)word[i];
if (b > 96){
if (b >= 110){
b = b - 13;
}
else {
b = b + 13;
}
output += ((char)b);
}
else
{
if (b >= 78){
b = b - 13;
}
else {
b = b + 13;
}
output += ((char)b);
}
}
cout << output << endl;
output = "";
cin >> input;
}
check = 0;
}
return 0;
}
如果没有更多的行可供输入,cin函数将返回false。您可以执行以下操作以读取直到输入结束,或者如果您将cin重定向到从文件读取,则可以执行eof。
int a;
while(cin >> a){
//Your loop body
}
您可以用以下行替换整个while循环:
std::getline(std::cin, input); // where input is a std::string
然后在这一行之后重新格式化。
相关文章:
- 在线编译器中的分段C++没有打印消息
- 在线程之间拆分任务总是值得的吗?
- join() 失败,如果在线程内部调用 io_context.run()
- 更新后的Android Studio现在需要Clang,不再链接到GCC的库
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 如何在C++中在线组装?
- 在线程中读取无符号整数时,c++ 位是否以原子方式切换?
- 编译器现在遵循C++14标准,我的项目不再编译神秘的SFML错误
- 不再支持Android NDK APP_STL gnustl_shared
- Visual Studio 2019:使用 vcvars64 从命令行构建C++.bat不再工作
- 在线程函数中处理数据向量时进行线程竞速
- 我应该在线程上调用 processEvents() 吗?
- 如何创建始终在线的过程?
- pthread只有在线程数量较少时才可以正常工作
- boost::asio::io_service 在线程中,不会在应用程序退出时退出
- 如果我从流中提取某些内容,该流是否不再包含我提取的内容?
- QObjects 被移动到 QThreads 后信号不再工作
- 如何在线程中初始化对象,然后在其他地方使用它?
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接
- WinAPI 窗口在使用 PrintWindow 截屏后不再更新