如何让字符串输入n次?
How to get string input n times?
我有一个 C++ 程序。我想从用户那里得到一个数字 (t
) 并强制用户输入行 t
次,但程序的执行在 1 次迭代后终止。这是代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int t;
cin >> t;
for (int i=0; i< t; i++) {
getline(cin, str);
cout << str;
}
return 0;
}
谁能解释一下为什么会这样以及如何解决?
谢谢我的朋友们。
当您输入第一个字符(重复次数)时,cin
缓冲区中会留下一个字符 - cin >>
不会消耗换行符。结果,getline(cin, str)
读取这个字符并将其作为第一个输入,然后清空缓冲区并让您输入其他字符。
You can clear the buffer with std::cin.ignore(1);
删除尾随字符 - 这让您的代码 运行 符合预期。为什么不直接使用 cin >> str
呢?这解决了问题并避免调用 getline
。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int t;
cin >> t;
//clear one character out of buffer
cin.ignore(1);
//note that 1 is used for demonstration purposes
//in development code, INT_MAX, numeric_limits<streamsize>::max(),
//or some other large number would be best, followed
//by std::cin.clear()
for (int i=0; i< t; i++) {
cout << "input: ";
//you could use cin >> str; instead of getline(cin, str);
getline(cin, str);
cout << "got: " << str << std::endl;
}
return 0;
}
当您执行 cin >> t
时,换行符仍在缓冲区中,因此您读取的下一行将是空白。当您混合使用格式化输入 (>>
) 和未格式化输入 (std::getline
) 时,您经常会遇到这样的情况,您需要在切换到未格式化输入时采取措施。补救措施示例:
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main() {
string str;
int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // skip the rest of the line
for(int i = 0; i < t; i++) {
if(getline(cin, str)) // check that the getline actually succeeded
cout << str << '\n';
else
break;
}
return 0;
}
我有一个 C++ 程序。我想从用户那里得到一个数字 (t
) 并强制用户输入行 t
次,但程序的执行在 1 次迭代后终止。这是代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int t;
cin >> t;
for (int i=0; i< t; i++) {
getline(cin, str);
cout << str;
}
return 0;
}
谁能解释一下为什么会这样以及如何解决? 谢谢我的朋友们。
当您输入第一个字符(重复次数)时,cin
缓冲区中会留下一个字符 - cin >>
不会消耗换行符。结果,getline(cin, str)
读取这个字符并将其作为第一个输入,然后清空缓冲区并让您输入其他字符。
You can clear the buffer with std::cin.ignore(1);
删除尾随字符 - 这让您的代码 运行 符合预期。为什么不直接使用 cin >> str
呢?这解决了问题并避免调用 getline
。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int t;
cin >> t;
//clear one character out of buffer
cin.ignore(1);
//note that 1 is used for demonstration purposes
//in development code, INT_MAX, numeric_limits<streamsize>::max(),
//or some other large number would be best, followed
//by std::cin.clear()
for (int i=0; i< t; i++) {
cout << "input: ";
//you could use cin >> str; instead of getline(cin, str);
getline(cin, str);
cout << "got: " << str << std::endl;
}
return 0;
}
当您执行 cin >> t
时,换行符仍在缓冲区中,因此您读取的下一行将是空白。当您混合使用格式化输入 (>>
) 和未格式化输入 (std::getline
) 时,您经常会遇到这样的情况,您需要在切换到未格式化输入时采取措施。补救措施示例:
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main() {
string str;
int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // skip the rest of the line
for(int i = 0; i < t; i++) {
if(getline(cin, str)) // check that the getline actually succeeded
cout << str << '\n';
else
break;
}
return 0;
}