I would like to have my application take either a parameter or read from stdin, and print that on the screen. But I'm having problems when trying to concatenate the strings.
I found out that "+" would do, but it's giving me problems so I attempted to use the append
method which is now throwing the error I'm showing below.
#include <iostream>
using namespace std;
std::string Read_stdin(){
std::string result="";
std::string input_line="";
while(cin) {
getline(cin, input_line);
result.append(input_line).append(endl);
};
}
int main(int argc, char** argv)
{
std::string input = (argc == 2) ? argv[1] : Read_stdin();
cout << "Your input is : " << input;
return 0;
}
Input:
Error:
jdoodle.cpp: In function 'std::__cxx11::string Read_stdin()':
jdoodle.cpp:13:46: error: no matching function for call to 'std::__cxx11::basic_string<char>::append(<unresolved overloaded function type>)'
result.append(input_line).append(endl);
^
In file included from /usr/include/c++/5.3.0/string:52:0,
from /usr/include/c++/5.3.0/bits/locale_classes.h:40,
from /usr/include/c++/5.3.0/bits/ios_base.h:41,
from /usr/include/c++/5.3.0/ios:42,
from /usr/include/c++/5.3.0/ostream:38,
from /usr/include/c++/5.3.0/iostream:39,
from jdoodle.cpp:1:
/usr/include/c++/5.3.0/bits/basic_string.h:983:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::append(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
append(const basic_string& __str)