c++ - Reading multiple lines from keyboard as input -
for code, had read multiple lines keyboard. code have here job. here code:
#include <iostream> using namespace std; int main() { string input; string line; cout<< "enter input line" << endl; while (getline(cin, line)) { if (line == "^d") break; input += line; } cout<< "the input entered was: "<<endl; cout<< input<< endl; }
the output after running this.
enter input line hello world ! input entered was: helloworld !
the problem: see, getline give white space when printing hello world. how make sure gets printed "hello world !" rather "helloworld !" happens when there n newline. concatenated previous line string , printed.
try this,
while (getline(cin, line)) { if (line == "^d") break; input += " " + line; }
Comments
Post a Comment