Active Topics

 


Reply
Thread Tools
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#1
Hi
Here's my code
Code:
int main() 
{
    clear();
    bool exit = false;
    string cmd, arg;
    while (exit == false)
    {
        cout << endl << "Type command. Type 'help' to see the comands." << endl <<
                        "> ";
        cin >> cmd;
        if (cmd == "help") showhelp(); 
        else if ( cmd == "b" || cmd == "w") wblist(cmd[0]); //as above (so below :D)
        else if (cmd == "exit") exit = true;
        else if (cmd == "restore") restore();
        else if (cmd == "backup") backup();
        else cout << "Incorrect command. Type again. Or use help by typing 'help'.";
    }
    return 0;
}
Is it possible to make input
Code:
b mediaplayer
with cin as 2 strings
but also make
Code:
exit
work

I tried

Code:
cin >> cmd >> arg;
But then I couldn't just type "exit"
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#2
The ">>" operator takes the line from cin and puts it into the string (as with lotion in a basement).

This means that if you want to handle the input "b mediaplayer" as two parts, you need to tokenize it yourself.

An easy way is to look at the method getline and use ' ' (space) as the line delimiter. This way you can split input data (but make sure to handle the case when there is no space) and do whatever you want with the second part.
 
Reply


 
Forum Jump


All times are GMT. The time now is 07:48.