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