terminating the loop with \n

I am making a class that reads a sentence then stores it. The teacher said that the loop should stop by looking for \n
Does this mean that the loop should stop when the user hits enter? (aka a new line?)
for example if my program says:
Enter sentence: hello my name is joe [enter]
or does it mean this
Enter sentence: hello my name is joe \n [enter]
I know how to make the second version. But how do I make a loop that stops when the user hits enter?
 
while (temp != "\\n") { cin >> temp; ....... }

is what I have, but I am not sure how to make it stop with the user hitting enter. any help will be appreciated thanks. BTW this is a series of strings it is reading in
"\\n" is "\n" string.
If you want to stop look when newline character is entered, you should do this instead:
[code]
temp != '\n'
[code]
(remember - it's newline CHARACTER, so you use ''.
But this isn't what you're looking for, I guess.

Answering your question - your program will actually do neither.
If you want to stop reading a string when user presses enter, I would simply use std::getline function. It will get you whole string up to enter(so, newline character) and store it in std::string that you provide it with.

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

This solution is doing exactly what you want.

Cheers!
Topic archived. No new replies allowed.