+ 2

Multiline input in console application

How can I read many lines of input into one string variable in the code playground? (I'm using C++) A message box specifies that we should write multiple inputs in separate lines, so I'm kind of lost. Please help.

17th Aug 2018, 10:16 AM
Moses Odhiambo
Moses Odhiambo - avatar
3 Answers
+ 2
Just replace the 2nd line with while( std::getline( std::cin, tmp ) ) result += tmp; However on (my) pc this doesn't open the input box for some reason. I usually work around it by placing #define a(x) std::cin >> x; on the very last line.
17th Aug 2018, 12:07 PM
Dennis
Dennis - avatar
+ 1
You could use a temp variable and a while loop to read from the input stream until there is nothing left and append it to the resulting string. e.g. std::string result, tmp; while( std::cin >> tmp ) result += tmp; std::cout << result; Input: Multiline input in console application output: Multilineinputinconsoleapplication
17th Aug 2018, 11:56 AM
Dennis
Dennis - avatar
+ 1
Dennis thanks, it works great! 👌
17th Aug 2018, 12:09 PM
Moses Odhiambo
Moses Odhiambo - avatar