Monday, February 8, 2010

My C++ program is returning false on a line, so it isn't performing an IF statement, and I can't tell why.

Here are the relevant lines of code.


while (! myfile.eof()){


if ((offset = line.find(search, 0)) != string::npos) {


cout %26lt;%26lt; ';found ''; %26lt;%26lt; line %26lt;%26lt; ';' @ offset '; %26lt;%26lt; offset %26lt;%26lt; endl;


curs = %26amp;line[28];


for (int n=0; n%26lt;10; n++){


starttime[n] = *curs - 48;


curs++;


cout %26lt;%26lt; starttime[n];


}


cout %26lt;%26lt; endl;


for (int i=0; i%26lt;10; i++){


start = start + (starttime[i]*pow(10,9-i));


}


search = ';%26lt;session type=\';stop\';';; // search pattern


if ((offset = line.find(search, 0)) != string::npos) {


cout %26lt;%26lt; ';found ''; %26lt;%26lt; line %26lt;%26lt; ';' @ offset '; %26lt;%26lt; offset %26lt;%26lt; endl;


curs = %26amp;line[27];


for (int n=0; n%26lt;10; n++){


stoptime[n] = *curs - 48;


curs++;


cout %26lt;%26lt; stoptime[n];


}


cout %26lt;%26lt; endl;


for (int i=0; i%26lt;10; i++){


stop = stop + (stoptime[i]*pow(10,9-i));


}


convo++;


}


search = ';%26lt;session type=\';start\';';; // search pattern


total += stop-start;


}


When debugging, the program goes through the first IF normally, but the second IF gets skipped.My C++ program is returning false on a line, so it isn't performing an IF statement, and I can't tell why.
So, it looks like you are parsing an XML file for %26lt;session type = ';start';%26gt; and stop.





I would guess they appear on different lines in the file. They way you have your code arranged is this:





if (line has start)


{


....if (line has stop)


....{


....}


}





Unless a line has both markers together, the second 'if' will never be true. What you want is something like this:





if (line has start)


{


}


else if (line has stop)


{


}





Also, you don't have to hand convert a text number to a binary number. There are numerous routines that will do this for you.





The older C way:


#include %26lt;stdlib.h%26gt;


#include %26lt;string.h%26gt;


char starttime[11]


strncpy(starttime, line+28, 10);


start = atoi(starttime);





Using c++ streams:


#include %26lt;string%26gt;


#include %26lt;sstream%26gt;


using istringstream;


using string;





istringstream timestream(line.substr(28, 10));


timestream %26gt;%26gt; start;My C++ program is returning false on a line, so it isn't performing an IF statement, and I can't tell why.
It would have helped to have some idea what the input looks like in order to check logic.





Is it the case that one input record contains both the search term in the first if stmt and also the ';stop'; search term in the second if stmt? They are nested.

No comments:

Post a Comment