Input problem.

Hey, I'm trying to take in strings as an input and place them in a vector, stopping when the user presses the return key.

The relevant part of the code looks like this at the moment:

1
2
3
4
5
while (...)
	{
		cin >> StringPasser;
		Vector.push_back(StringPasser);
	}


I need something that recognises the end condition (ie. \n input) and breaks. I tried making the while loop finish when StringPasser = the return character, but that didn't work.
Last edited on
Could you be more specific about the problem?
Is StringPasser a char or a string?
Vector is of type char or string?

Are you trying to take input in the form of per character?
1
2
3
4
while(cin && cin.peek() != '\n')
{
   // ...
}

But it works only when the last string is followed with the new-line symbol without any spaces. If you want the last, you should do it "with your arms" by for cycle.
Could you be more specific about the problem?
Is StringPasser a char or a string?
Vector is of type char or string?


Sorry, should have specified.

Both are of type string, ie. initialised as:

1
2
string StringPasser
vector<string> Vector


Are you trying to take input in the form of per character?
But it works only when the last string is followed with the new-line symbol without any spaces.


I want it to take the input as whole words, or sentences (which it will currently do), but to end when the return key is pressed, irrespective of what precedes it.
@Luke Victory:
You should use getline() function to take input rather than cin.

You can use it as:
1
2
getline(cin,StringPasser);
Vector.push_back(StringPasser);
Ah right. Yep, that would work. Thanks.
Topic archived. No new replies allowed.