I've been building a shell for a while now, and I decided to implement the echo function. However, no matter how much I tried, it returned nothing (literally, nothing. Just blank.). I looked through a ton of different references, but most strings did not include a space, mine typically will.
Edit: The globals, are, indeed, pointless, and were put there to make it easier to copy/paste directly into the shell I was writing, which relied heavily on them.
#include <iostream>
#include <string>
using namespace std;
static string argument;
string echo(string echoarg)
{
string echoreturn = echoarg.substr(4);
return echoreturn;
}
int main()
{
string argument;
cin >> argument;
cout << echo(argument);
return 0;
}
No, your string won't include a space, because cin >>
stops at the first space.
Try
getline(cin, argument);
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.