Конкретно, это будет зависеть от того, откуда поступает входной сигнал, но обычно я подхожу к нему с разбором. Примерно так:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
// Prototype
void runCommand(std::string action, int amount);
void parseCommand(std::string rawCommand, std::vector<std::string>& command);
int main()
{
std::vector<std::string> command; // Vector of strings that will take the command
std::string stringInput;
parseCommand(stringInput, command); // Separate the raw command into different strings (command[0] and command[1])
runCommand(command[0], stoi(command[1])); // Run the command
return 0;
}
void runCommand(std::string action, int amount)
{
if (action == "add") // If the command is to add, call the add function
{
// add(amount);
}
else if (action == "cancel") // If the command is to cancel, call the cancel function
{
// cancel(amount);
}
}
// This function will take the string rawCommand, parse it into different strings,
// and put the parsed string into the vector command. Command is taken by reference
// so it can modify the original vector object.
void parseCommand(std::string rawCommand, std::vector<std::string>& command)
{
std::stringstream stringStream; // Convert the string input to a std::stringstream
std::string stringTransfer; // This string will be a part of the command input
stringStream << rawCommand; // Converting the stringInput into a stringStream
// parse the line separated by spaces
while (std::getline(stringStream, stringTransfer, ' '))
{
// put the command in the vector
command.push_back(stringTransfer);
}
// Clear the command to get it ready for the next potentintial command
command.clear();
}
Конечно, вам придется объявить все переменные. Я сам нуб, поэтому у кого-то еще может быть лучший метод.