0

I'm trying to create a program to separate a single line into a vector of strings separated by the blank spaces in said line, so turn:

foo bar

into

["foo", "bar"]

This is what I have so far:

string command;
string command_temp;
vector<string> command_seperated;

std::cin >> command;

for (int i = 0; i < command.length(); i++){
    if (isspace(command[i])){
        cout << "blankspace" << endl;            command_seperated.push_back(command_temp);
        command_temp.clear();
    }

    command_temp.push_back(command[i]);
    for (int i = 0; i < command_temp.size(); i++){
        cout << command_temp[i];
    }
    cout << endl;
}

for (int i = 0; i < command_seperated.size(); i++){
    cout << command_seperated[i] << endl;
}

But, if I input "foo bar" when prompted, this just returns:

foo bar
f
fo
foo

Process returned 0 (0x0)   execution time : 2.596 s
Press any key to continue

I assume the reason the last for loop isn't printing anything is that there's nothing in it and the push_back to command_seperated isn't working. I have no idea why.

I also don't know why the entire program seems to just stop working after the first blank space.

Using this to refresh my rudimentary C++ skills, so I would appreciate an explanation of why I'm wrong, rather than a more elegant alternative solution.

4
  • 3
    The reasons can easily be discovered by stepping through the code with a debugger, did you try that? Commented Sep 1, 2021 at 20:17
  • 7
    std::cin >> command stops when it encounters whitespace. Perhaps you want std::getline instead of >>? Commented Sep 1, 2021 at 20:18
  • Yep, what @RetiredNinja said about cin is true. I think the code works otherwise. Maybe also put a continue; in the isspace conditional, so it doesn't add the space to command_temp. Commented Sep 1, 2021 at 20:28
  • comment comment Commented Sep 1, 2021 at 20:53

1 Answer 1

2

What you are seeing is normal behavior of operator>>, which is used for reading formatted input. It skips leading whitespace (if the skipws flag is enabled on the stream, which it normally is), then reads until EOF or whitespace is encountered. So, in your example, std::cin >> command receives only foo even though you entered foo bar. If you invoked std::cin >> command a 2nd time, you would receive bar.

To read a string that contains whitespace in it, use std::getline() instead:

std::getline(std::cin, command);

It does not skip leading whitespace, and reads until EOF or a linebreak (ie, from typing Enter) is encountered.

That being said, the parsing code you have shown can be greatly simplified if you use a std::istringstream with operator>> to parse the command, eg:

std::string command, token;
std::vector<std::string> command_seperated;

std::getline(std::cin, command);

std::istringstream iss(command);
while (iss >> token) {
    command_seperated.push_back(token);
}

for (const auto &str : command_seperated){
    cout << str << endl;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.