Friday, July 31, 2009

C++ How do you search through a string for a newline statement?

int count = 0;


string first;


while (text[count] != '/n')


{


first.push_back(text[count]);


count++;


}


cout %26lt;%26lt; endl %26lt;%26lt; first;





My compiler says I can't use the while statement because it is always true.

C++ How do you search through a string for a newline statement?
Hi - you're missing a "/" in your while statement.





the "/" statement is an escape character, and if you just say "/n", it sees it as part of the string. It's an impossible pattern in the string class, so it's always true. Try looking for "//n".





good luck -





Rob
Reply:Depends if you're using the String datatype or if you're using a character string thats ended with "/0" (I think). In your case it looks like you've got a character string.





You should be able to do:





int count = 0;


while( text[count] != '/0" )


{


if( text[count] == '/n' ) cout %26lt;%26lt; endl;


count++;


}





Or something like that. Not really sure what your objective is here.


No comments:

Post a Comment