【发布时间】:2017-01-25 05:16:08
【问题描述】:
while(getline(data, word, '\n')){//seperates by line
ss<<word; // output1: Chicken, for sale, 60
// output2: Microwave, wanted, 100 (and so on)
while(getline(ss, word, ',')){//seperates by individual words + space
// output1: Chicken
// output2: for sale
// output3: 60
if(word[0]==' '){ //removes space in 2 and 3
word.erase(0,1);
}
if(wordindex==0){
board[i].object=word;
wordindex++;
}
else if(wordindex==1){
board[i].type=word;
wordindex++;
}
else if(wordindex==2){
board[i].price=word;
wordindex=0; //resets index to 0 for next line
i++; //moves to next struct in array
}
}
}
第二个 getline 循环只为第一个输入循环一次:chicken, for sale, and 60,并且不会到达第二个。我认为单词索引始终设置为0,所以这应该不是问题。此外,第一个getline() 完全输出所有数据,所以有些东西导致第二个getline() 感到困惑。我只是看不到它是什么。
【问题讨论】:
-
您可能会发现,几乎可以肯定,您的开发环境附带的调试软件可以非常有效地找出问题所在。
-
另外,另一位用户之前提出了一个类似解析问题的问题。事实证明,在空格上使用
>>解析并去掉逗号更容易。一方面,它将整数保持为整数。 stackoverflow.com/a/41840993/4581301 -
我的解析其实很简单。
标签: c++