1

I'm trying to take a string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector >& split(std::string&, char, std::vector >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>

using namespace std;

vector<string> &split(string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

int main(){

    string input;
    cin>>input;
    vector<string> splitInput;

    split(input, " ", splitInput);

    for(int i = 0; i < splitInput.size(); i++){
        cout<< splitInput.at(i) << endl;
    }

    return 0;
}
Community
  • 1
  • 1
a.sapp
  • 41
  • 1
  • 9
  • 1
    Make it `split(input, ' ', splitInput);`. Note single quotes in place of double quotes. Read about character literals and string literals in your favorite C++ textbook. – Igor Tandetnik Sep 20 '15 at 20:37

1 Answers1

1

Change this

split(input, " ", splitInput);

to this

split(input, ' ', splitInput);

since the prototype of the function is:

vector<string> &split(string &s, char delim, vector<string> &elems) ;, which requests for a char as the second argument, NOT a string.

" " is a string-literal which has type const char[2], while ' ' is a character, which has type char.

You can also check this question: What is the type of string literals in C and C++?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Ahh thank you! I still trying to get the hang of C++ and I forgot that ' ' is different from " ". Still used to writing in Java. I fixed that, but now it's only placing the first character from the string into the vector and none of the others. – a.sapp Sep 20 '15 at 20:51
  • You are welcome!Try printing what your variable has at every step and debug your program. If you have problems, post a new question, happy debugging! :D – gsamaras Sep 20 '15 at 20:52
  • So I printed out item in every iteration of the while loop in the split method, and it's only running the loop once. Which means that getline(ss, item, delim) is returning false after one iteration. I looked up the reference for getline, but I still don't really know why it's returning false after one iteration. – a.sapp Sep 20 '15 at 21:07