c++ - string::std find method always find a character, even when it's not in the string -


i'm doing project school parsing through strings passed in text file. however, i'm having problems string.find. when run code:

string thefile; while (infile) {     infile.getline(line, 512);     thefile = line;     bool parsing = true;     while (parsing){         choice *newchoice = new choice;         if (thefile.find("|")!= -1) {             newchoice->lines = thefile.substr(thefile.find("%") + 1, thefile.find("|") - 1);              thefile.erase(0, thefile.find("|") + 2);              tempnpc->choices.push_back(newchoice);          }         else            parsing = false; 

the input looks this:

| d1% stuff | d2% more stuff | 

my problem: if statement never false. once thefile empty, still runs code , out-of-bounds error. idea why find("|") isn't working?

*edited add how string file

you might want @ documentation, can find out return values std::string::find mean. specifically, if you're not sure value present should compare npos before using it, not "-1". , problem continuing run after input's exhausted best solved in input code don't show, rather trying parse string may or may not actual input.

update: speculated in comment vsoftco below, input logic broken. use this:

while (getline(infile, thefile))     ... 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -