-First post Woot!-

Alright, here's the problem; I'm building a _very_ simple Terminal-like program. The only problem is that in a normal Terminal (be it in Linux or Windows' "Command Prompt") there is no definitive number of inputs that you can input, for instance; you can either type "dir", or "echo BLAH BLAH BLAH", and both would work. But how would I do this in C++? When I tried to do:

cin >> arg1 >> arg2 >> arg3 >> endl;

It flipped a bitch when it didn't have _all_ of the inputs specified. And when I tried:

getline (cin, arg1);

I may have been able to put in as many arguments as I wanted, but I didn't have as much control over them as I had had before. For instance; I used to be able to type:

cin >> arg1 >> arg2 >> endl;
if(arg1 == "echo")
{
    cout << arg2;
}

But I can't do that with getline().

The only other ways I could think of solving this problem, is by either writing all inputs to a text file, reading them, _then_ processing them. Or somehow splitting the inputs into tokens, and _then_ processing them. But I feel that I'm side stepping something blindingly obvious, as I am still a noob at C++. Help?

Thanks, and sorry for that wall of text XD
~Mr. Appleseed

(On a side-note, I _did_ search to see if any of this had been posted before, and though there _were_ similar posts, they were all for numbers instead of strings.... And as far as I know, you can't put a string in an 'int' ^.^ )

Recommended Answers

All 8 Replies

You threw out the correct answer already. It's getline() . You read the line and parse the values from it.

1) read the line (say grab val1 var2 freak3
2) retrieve the first value, the command (grab)
3) remove the first value leaving the parameters (val1 var2 freak3)
4) execute the proper command section passing to it the parameter string

In the command processor
1) Parse out the parameters
2) check them for legality
3) execute the command based on the parameters

The stringstream class is your best friend for this purpose.

Use the getline() function to obtain the string for the entire line, then pass that string to a stringstream object and then extract the tokens from the stringstream.

For dealing with multiple and variable-number of arguments, you can do what WaltP suggested, you take each token one at a time and figure out what tokens you should see next, and so on.

I could also recommend that you make a queue of strings in which to align all the tokens that you read from the input. Then, your command-handlers can simply inspect the next tokens on the queue and read off as many as they need or recognize (as options).

Other libraries that can help you is the Boost.Tokenizer, Boost.Regex (or new standard Regex library), and Boost.Spirit/Phoenix (brace yourself!).

Thanks! I'll defiantly work on it and respond with my findings! =)

~Mr. Appleseed

Hey again, I didn't want to make another thread, so I just decided to put it here. Hope that's ok.

I'm having issues with processing the tokens that I've made. So far, I can make tokens so that the word "the dog is awesome" for instance, would become;
"the"
"dog"
"is"
"awesome".

My only problem being that once I have this, I can't do anything with it. I tried comparing it to a string literal, but char's don't play nice with string literals. Then I tried using switch statements, I learned that you can't use strings in those. I'm currently out a loss of what to do, and what to Google. I'm not asking for source code, as much as I'm asking for a term to Google. Here's my current code:

char uInput[256];
char* token; 
char uToken[256];
string tokenString(uToken);

void Tokenizing()
{
    /* Tokenizing time */

    cin.getline(uInput, 256);
    token = strtok(uInput, " ");
    while(token != NULL)
    {
        strcpy(uToken, token); 
        token = strtok(NULL, " ");
    }
}

strcmp()

> I'm having issues with processing the tokens that I've made.

Make the task easier by using std::string.

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

int main()
{
    std::string line ;
    while( std::cout << '>' && std::getline( std::cin, line ) )
    {
        // tokenize the line on white space
        std::string token ;
        std::istringstream stm(line) ;
        while( stm >> token )
        {
            std::cout << "token: " << token << '\n' ;
            if( token == "this_token" )
            {
                // ...
            }
            else if( token == "some_other_token" )
            {
                // ...
            }
            // etc
        }
    }
}

Too late, already made char array tokens :P

Thanks WaltP! Finally got it :D

Too late, already made char array tokens

It's never too late to refactor your code into something better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.