0

I'm a little new to C++, so sorry if this question is obvious, but I've hit a bit of a roadblock. The thing I want to do is have a command prompt that does certain things. You put in simple commands like timer down 10 which will start a timer count down which I have done fine. The way I'm detecting for each word is with this:

string cmd1;
string cmd2;
int cmd3;
cin >> cmd1 >> cmd2 >> cmd3;

That works fine, except I want to have single-word commands and with this system, I can't really do that. If I want, for example, help as a command, its making me type 2 strings and an int when I only want to type 1 string. But I want to have specific commands that can be the full 2 strings and an int or just 1 string.

  • I'd recommend reading everything into a string, and then do parsing after. You can use std::getline. – Trevor Hickey Oct 07 '17 at 04:05
  • Trevor Hickey I did consider that, but I'm not sure how to get read a specific word. Any suggestions for how to do that? Thanks – TriDeapthBear Oct 07 '17 at 04:15
  • @TriDeapthBear What? As was suggested in the comment - read entire line with `std::getline`, and then do parsing on that line afterwards. What's unclear about that? – Algirdas Preidžius Oct 07 '17 at 05:25
  • [Most elegant way to split a string](https://stackoverflow.com/q/236129/995714) – phuclv Oct 07 '17 at 05:42
  • 2
    Alternatively, just read `cmd1` first, check which command that is, and then optionally read the parameters if required for that command. – Bo Persson Oct 07 '17 at 06:06

3 Answers3

0

You need to read the command with getline then split it to tokens. Check for the getline function, and google for split line to tokens c++ .

user31264
  • 6,557
  • 3
  • 26
  • 40
0

Use getline to store whole command in a single String.

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

Now, you can split the command into token words using following code.

int counter =0;
string words[10];
for (int i = 0; i<command.length(); i++){
    if (command[i] == ' ')
        counter++;
    else
        words[counter] += command[i];
}
Suryakant Pandey
  • 324
  • 2
  • 10
0

You could read the input line by line and then split each line into a std::vector containing each command followed by its arguments:

void command_help()
{
    // display help
}

void command_timer(std::string const& ctrl, std::string const& time)
{
    int t = std::stoi(time);

    // ... etc ...
}

int main()
{
    // read the input one line at a time
    for(std::string line; std::getline(std::cin, line);)
    {
        // convert each input line into a stream
        std::istringstream iss(line);

        std::vector<std::string> args;

        // read each item from the stream into a vector
        for(std::string arg; iss >> arg;)
            args.push_back(arg);

        // ignore blank lines
        if(args.empty())
            continue;

        // Now your vector contains

        args[0]; // the command
        args[1]; // first argument
        args[2]; // second argument
        // ...
        args[n]; // nth argument

        // so you could use it like this

        if(args[0] == "help")
            command_help(); // no arguments
        else if(args[0] == "timer")
        {
            if(args.size() != 3)
                throw std::runtime_error("wrong number of arguments to command: " + args[0]);

            command_timer(args[1], args[2]); // takes two arguments
        }
    }
}
Galik
  • 47,303
  • 4
  • 80
  • 117