"Ben" <bw****@gmail.com> wrote in message
news:da*************************@posting.google.co m...
Hello,
I'm having trouble with something that I imagine should be simple. I
am trying to write a program that will read in a sentence that the
user types (that will contain white space), then parse the sentence.
I do not know how long the sentence will be, so it doesn't look like
cin fits well with what I want to do. Any suggestions?
Use std::string and std::getline. It's the best way to flexibly read lines
of text in C++. Please respond if it does not suit your needs.
#include <iostream>
#include <string>
int main() {
std::string line;
while( std::getline(std::cin,line) ) {
std::cout << "You entered '" << line << "'" << std::endl;
}
return 0;
}
Output:
asdf asdf asdf
You entered 'asdf asdf asdf'
asdfa asdfasdfasdfasdf asdfasdfasdf
You entered 'asdfa asdfasdfasdfasdf asdfasdfasdf'
hey there
You entered 'hey there'
a
You entered 'a'
You entered ''
asdf
You entered ' asdf'
--
David Hilsee