错误:无法在赋值

时间:2015-07-28 14:23:27

标签: c++

我遇到了问题 - 我想将标准输入行的值存储到int但不确定如何转换为int:

string line;
int value;
getline(cin,line);
istringstream ss(line);
while (ss>>line) {
    if (ss.eof()==false) {
        // stores non ints in strings
    }
    else {
        value=line; //ERROR
    }
}

我试图使用标准文档材料转换它,但我无处可去。我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可以使用std::stoi

value=std::stoi(line);

答案 1 :(得分:0)

You could use your istringstream to try loading a value into an int while checking for ss.fail().

ss >> temp; // where temp is an int
if(ss.fail())
{
// handle the error because the value wasn't an int
}
else
{
// process your int
}

This, naturally, could be modified a bit based on your needs.