有几种方法可以解决你的问题。最简单的可能是移出一个直接的cin/cout循环,而是使用std :: getline。具体来说,你可以写这样的:
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main(int argc, char **argv)
{
vector<int> reftest;
while (true)
{
string input;
getline(cin, input);
// You can do whatever processing you need to do
// including checking for special values or whatever
// right here.
if (input.size() == 0) // empty input string
{
cout << "Assuming you're bored with the Entering Numbers game." << endl;
break;
}
else
{
int value;
// Instead of using the input stream to get integers, we
// used the input stream to get strings, which we turn
// into integers like this:
istringstream iss (input);
while (iss >> value)
{
reftest.push_back(value);
cout << "Inserting value: " << value << endl;
}
}
}
}
其它方法包括cin.getline()(这我不是的,因为它的工作原理的*不是字符串的字符的大风扇),使用cin.fail( )位以确定传入值是否有好处等。根据您的环境,获取用户输入的方式可能比通过iostream更丰富。但是这应该指向你所需要的信息。
我宁愿用户能够简单地输入他们的所有号码,而不是在每次输入后要求更多。 – Alex 2009-07-05 18:17:18