Is there anyway to use the getline() function with strings seperated by \n or endl???
Thanks,
Terrance
Is there anyway to use the getline() function with strings seperated by \n or endl???
Thanks,
Terrance
sorry, incomplete post.
i.e., could I turn a string into a stream, and use the getline function like this:
getline(stringToStream, stringName);
Is there anyway to turn a string into a stream, seperated by the \n, endl;
Thanks
Yes, there is. Here's how the apstring class does that.Originally posted by Terranc
i.e., could I turn a string into a stream, and use the getline function like this:
getline(stringToStream, stringName);
Is there anyway to turn a string into a stream, seperated by the \n, endl;
Thanks
Now, you're probably going to have to finagle that to get it to work with the type of string you're using. To use the function above, call it like so:Code:istream & getline(istream & is, apstring & str) { char ch; str = ""; while(is.get(ch) && ch != '\n') str += ch; return is; }
If you need help getting that to work with your type of string(char[], string, etc.), just let us know what that is, post your code and a specific question, and we'll try to help you out.Code:getline(cin, string_variable);
I hope that answered your question
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
>i.e., could I turn a string into a stream, and use the getline function like this:
It sounds like you want a stringstream:
-PreludeCode:#include <iostream> #include <sstream> int main() { std::string s = "Stuff up to the newline\nJunk after the newline"; std::string a; std::istringstream iss ( s ); std::getline ( iss, a ); std::cout<< a <<std::endl; }
My best code is written with the delete key.
thanks for the help, got my thing to work