c++ - Set std:cin to a string -


for ease of testing wish set cin's input string can hardcode.

for example,

std::cin("test1 \ntest2 \n"); std::string str1; std::string str2; getline(cin,str1); getline(cin,str2);  std::cout << str1 << " -> " << str2 << endl; 

will read out:

test1 -> test2 

the best solution imo refactor core code function accepts std::istream reference:

void work_with_input(std::istream& is) {     std::string str1;     std::string str2;     getline(is,str1);     getline(is,str2);      std::cout << str1 << " -> " << str2 << endl; } 

and call testing like:

std::istringstream iss("test1 \ntest2 \n");  work_with_input(iss); 

and production like:

work_with_input(cin); 

Comments