[英]C++, cin until no more input on line using a while loop
我的C ++程序有问题。 我重新格式化并显示用户输入到控制台中的单词。 如果用户输入:嗨,我是鲍勃。用户将鲍勃输入控制台后,将按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;
}
您可以用以下行替换整个while循环:
std::getline(std::cin, input); // where input is a std::string
然后在这行之后重新格式化。
如果没有更多行可输入,cin函数将返回false。 您可以执行以下操作来读取直到输入结束,或者如果要重定向cin以从文件读取,则可以执行eof。
int a;
while(cin >> a){
//Your loop body
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.