Reading Multiple Lines from String

I'm trying to read multiple lines from a string that the user inputs.
For example, the user inputs two lines of strings.
adnasndansdas
asdnasndonasodn

I'm trying to get those two lines in just one string. Then I want to be able to return both of those lines later. The problem with it is that whenever I use a while loop or for loop two read bot or more lines only the last line is return.
Below is a shot example of what I'm doing and what I want.

1
2
3
4
5
6
string n;
for(int i=0;i<2;i++)) //this allows user to input no more than 2 lines.
{
getline(cin, n);
}
cout << n << endl  //Outputs the last line. I want both lines to be returned. 
Hello joe809,

I think something like might work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
	std::string str, bigString;

	for (int i = 0; i < 2; i++) //this allows user to input no more than 2 lines.
	{
		std::cout << "\nEnter a string: ";  // <--- Added. This does need a prompt.
		std::getline(std::cin, str);

		bigString += str + "\n";
	}

	std::cout << "\n\n" << bigString << std::endl;  //Outputs the last line. I want both lines to be returned.

	return 0;  // <--- Not required, but makes a good break point.
}

Line 13 is the most important. The rest is a suggestion.

Andy
H
Last edited on
I got it working to return all lines. I placed the for loop inside of the while loop in int main(). The problem with that now is that I get both inputs in the wrong order. Instead of getting something like,
hello
world

I get,
world
hello
Topic archived. No new replies allowed.