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.
#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.
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