0
我有以下代码:C++将字符串转换为整数;获得奇怪的输出?
{
line.erase(remove_if(line.begin(), line.end(), ::isspace), line.end()); //removes whitespace
vector<string> strs;
boost::split(strs, line, boost::is_any_of("="));
strs[1].erase(std::remove(strs[1].begin(), strs[1].end(), ';'), strs[1].end()); //remove semicolons
if(strs[0] == "NbProducts") { NbProducts = atoi(strs[1].c_str());
istringstream buffer(strs[1]);
buffer >> NbProducts;
}
但每当我试着和输出NbProducts我得到一个真正的随机数看。顺便说一下,输入来自用单行读取的文本文件:
"NbProducts = 1234;"
没有引号。
我知道现在的代码有点马虎。但是,任何人都可以立即看到为什么我会在“NbProducts?”的地方出现奇怪的整数。
你试图解析文本语法?你可能应该[用XY表示你的问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。 – sehe 2014-10-21 23:31:07