0

I am trying to create a command line app, where the user can type in commands and data, but I don't really get how istream_iterator is working, how can I get a whole input (until enter) into a vector? Right now it creates a new while loop on every word, that is not what is want.

int main(int argc, char* argv[])
{
    string buffer;      
    //vector<string> vbuff;
    CliHandler clihandler(argc, argv);
    int state = clihandler.State();
    while (state != CliHandler::STATE_EXIT) {

        cout << ">>";
            //Beolvasás
            cin >> buffer;
            stringstream sstream(buffer);
            istream_iterator<string> begin(sstream);
            istream_iterator<string> end;
            vector<string> vbuff(begin,end);

            copy(vbuff.begin(), vbuff.end(), std::ostream_iterator<string>(std::cout, "\n"));//test

            //vbuff = vector<string>((istream_iterator<string>(cin)), istream_iterator<string>());
            //copy(vbuff.begin(), vbuff.end(), std::ostream_iterator<string>(std::cout, "\n"));

            switch(clihandler.State(vbuff[0])) {
                          // [command] [data1] [data2] ...
            }
    }

    return 0;
}
szab.kel
  • 2,356
  • 5
  • 40
  • 74
  • I assume you got this this code from the stackoverflow question [How to split a string in C++?](http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c). I suggest you use [this answer](http://stackoverflow.com/a/236803/1750541) as it's much more scalable. – Ian Nov 19 '13 at 16:33
  • I tried, but I don't know how to use the function. Why do you need a const string to use it, i don't get it. – szab.kel Nov 19 '13 at 16:50
  • From what I can tell, you're trying to write code that will split a string. i.e. it will take a long series of characters a split it up into a vector of smaller chunks. However your description at the top talks about reading in user input. Are you certain that you've thought through what you're trying to do? I can see two possibilities: 1) Read input, split input up and store, end. Or 2) Read input, store input, read more input, store input, etc etc – Ian Nov 19 '13 at 16:58
  • I am reading input interactively. exit command would leave the while loop and end the program. – szab.kel Nov 19 '13 at 17:42

3 Answers3

0

Why don't you just use the argc and argv parameters? Something like this..(haven't tested)

int main(int argc, char* argv[])
{
    vector<string> vbuff(argc);
    for (int i = 0; i < argc; ++i)
    {
        vbuff[i] = argv[i];
    }

    // From here, you can use vbuff for your own purposes.
}
yyoon
  • 3,555
  • 3
  • 21
  • 17
  • Because the program would end right after, but I want to let the user to give multiple commands after another, while i got no "exit" command. – szab.kel Nov 19 '13 at 16:35
  • Oh, so you want to get user input interactively, not as command line parameters. I think I misunderstood your question. – yyoon Nov 19 '13 at 16:38
0

I m not very sure what u want(my poor english..), maybe you want to get input of the whole line until enter I think you can use cin.getline

    char mbuf[1024];
    cin.getline(buffer,1024);
Patato
  • 1,464
  • 10
  • 12
0

Based on your comment: "I am reading input interactively. exit command would leave the while loop and end the program"

You'd be better off getting that simple loop to work first, before trying to process the input string.

std::string inputCommand;
while(inputCommand != "Exit")
{
    cin >> inputCommand;
    //do stuff with it
}

Then you could consider splitting and handling the string

bool shouldExit(false);
std::vector<std::string> inputsReceived;
while(!shouldExit)
{
    char delim ('#'); //here put whatever character your inputs are separated by
    std::string buffer;
    cin >> buffer;
    std::stringstream ss;
    ss << buffer;
    std::string item;
    while (std::getline(ss, item, delim))
    {
        if (item == "Exit") //case sensitive
        {
            shouldExit = true;
            break;
        }
        else
        {
            //do whatever with input
        }

        //if you want to keep a record of the inputs in a vector
        inputsReceived.push_back(item);
    }
}
Ian
  • 450
  • 4
  • 18