简体   繁体   中英

C++ - Storing user input string separated by commas into vector

I have a code written that performs this task to a certain extent. But, I would like to how to alter my code so that I can store as many string inputs the user wants to enters into the vector.

Here is my code:

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

int main ()
{
string input = "";
cout << "Input: ";
cin >> input;
string a,b;

for(int i = 0; i<input.size(); i++)
{
    if(input.at(i)==','){
        a=input.substr(0,i);
        b=input.substr(i+1);
    }
}

vector<string> objects;
objects.push_back(a);
objects.push_back(b);

for (int k = 0; k < 2; k++) {
    cout << objects[k] << endl;
}

return 0;
}

So far, it can only recognize and store two inputs separated by commas. I am very new to coding so could someone show me a way to make this into a loop and take in as many inputs as the user enters?

Thank you.

There are much simpler approaches to parse an input string using stringstreams:

string a;
vector<string> objects;

for(stringstream sst(input); getline(sst, a, ','); )  // that's all ! 
    objects.push_back(a);

copy (objects.begin(), objects.end(), ostream_iterator<string>(cout," ; "));  // display all

Online demo

You need to change your code in order to work for any number of user input. The logic is to push every sub string between the commas into vector .

vector<string> objects;

for(int i = 0,j=0; i<input.size(); i++)
{
    if(input.at(i)==',' || input.at(i)=='\0'){
        objects.push_back(input.substr(j,i-j)); //pushing the sub string
        j=i+1;
    }
}

In order to print the vector first you have to find the size of the vector,then simply iterate over to print it.

//display

int l=objects.size();
for (int k = 0; k < l; k++) {
    cout << objects[k] << endl;
}

Note: If you want your code to work for strings with spaces in between , for example: a ,b ,c ,d then use getline(cin,input); to take input from user.

You can see running code here or as a github gist .

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <string>

void ParseCSV(
    std::vector< std::string >& output,
    const std::string& csv )
{

    int q = 0;
    int p = csv.find(",");
    while( p != -1 )
    {
        output.push_back( csv.substr(q,p-q) );
        q = p+2;
        p = csv.find(",",q);
    }

    // The terminating comma of the CSV is missing
    // so we need to check if there is
    // one more value to be appended

    p = csv.find_last_of(",");
    if( p != -1 )
    {
        output.push_back( csv.substr( p+2 ) );

    }
    else
    {
        // there was no comma
        // this could be because the list is empty
        // it could also be because there is just one element in the list

        if( csv.length() > 1 )
            output.push_back( csv );
    }
}

int main()
{
    std::string test("this is my list, a, b, c, d, end of line");
    std::vector< std::string > split;
    ParseCSV( split, test );
    for( auto& s : split )
        std::cout << s << std::endl;

}

As suggested by Christophe, using stringstream is much better. No special case handling needed! I use a while loop - it seems clearer what is happening.

void ParseCSV2(
    std::vector< std::string >& output,
    const std::string& csv )
{
    std::stringstream sst(csv);
    std::string a;
    while( getline( sst, a, ',' ) )
        output.push_back(a);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM