I have console aplication, user is typing command through console and I should do some action according those inputs. One command is on one line only.
for example cp is a command which has 4 parameters, between every 2 parameters can be many whitespaces they doesnt matter, so valid command is e.g.-> cp 4 5 6 9 thats valid.
valid command is new line too, it is not supposed to do anything just a know new line
valid command is, any number of white spaces and new line, it does the same thing as new line. But here is my problem, in my code where I am spliting strings according whitespaces it does SIGSEV and I dont why.
So i would like to know wheres the problem and how to fix it, or how can read user input effectively than I do now.Code:void io::start(std::istream& in, std::ostream& out, std::ostream& err){
bool end = true;
out << ">>>";
while(end){
vector<string> tokens;
string input;
getline(in,input); // read the line from console
if(input.size()==0) //if input was just newLine than i add a string zero
input += '\0';
stringstream ss(input); // Insert the string into a stream
string buf;
if(!has_only_spaces(input)){ //has_only white spaces is method which tries to not let string only with whitespaces to be splitted, but sigsev occurs in it too
while (ss >> buf){ //SIGSEV here, if theres no if around it, spliting the string here
tokens.push_back(buf);
}
}
}
bool has_only_spaces(const std::string &str)
{
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
{
char c = *it;
if(!isspace(c)){ //sigsev here on last char if string is " " if string is " a" everything is fine
return false;
}
}
return true;
}