1
所以我试图做一个只接受整数的循环,它起初工作正常,但只要不是整数的东西被输入串流,它将不断跳过如果声明即使使用了正确的输入。我错过了什么吗?检查输入是否是整数循环
# include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string str;
stringstream ss;
int a;
for(;;)
{
getline(cin, str);
ss<<str;
if (ss>> a)
{
cout<<"okay";
break;
}
cout<<str<<endl;//debugging (prints the correct input
str.clear(); //
ss>>str; //
cout<<str<<endl;// doesn't print anything but the endl space
cout<< "try again";
ss.str("");
}
}
oops额外{来自复制/粘贴错误。我在for循环中移动了ss.str(“”),但我仍然遇到同样的问题。 – user2533259