I'm currently adding a new feature "/msg" to a game that I'm modifying. The command works like "/msg name message". However, I'm having issue to identify which is the name and which is the message. Is using substring function possible to create this command?
"/msg name message"
How do I obtain string name and string message?
I've read all the tutorials on this forum on how to find string between 2 spaces using substring, but I still don't get it...
If you must use substr, here's one way to do it.
In the end, you'll get a string for "username", and a string for the actual message itself. Do with that what you will.
// Example program
#include <iostream>
#include <string>
///
/// Parses a command sent to see if it's a /msg command,
/// and if so, returns true and puts the username and message to be sent
/// into username_out and message_out, respectively.
///
bool parse_message_command(const std::string full_command, std::string& username_out, std::string& message_out)
{
const std::string msg_token = "/msg ";
if (full_command.substr(0, msg_token.size()) == msg_token)
{
// assumes username immediately starts at index[5]
size_t space_after_username_index = full_command.find(" ", msg_token.size());
if (space_after_username_index != std::string::npos)
{
username_out = full_command.substr(msg_token.size(), space_after_username_index - msg_token.size());
message_out = full_command.substr(space_after_username_index);
if (message_out.size() >= 1)
message_out.erase(0, 1); // pop initial space off, if any.
return (message_out != "") && (username_out != "");
}
else
{
returnfalse;
}
}
returnfalse;
}
int main()
{
// get the username and message from user input
std::string full_command;
std::cout << ">";
std::getline(std::cin, full_command);
// variables to store the username and message in
std::string username;
std::string message;
// get the username and message
if (parse_message_command(full_command, username, message))
{
// use the username and message
std::cout << "Sending message \"" << message << "\" to \"" << username << "\"..." << std::endl;
}
else
{
std::cout << "Unknown command or incorrect syntax" << std::endl;
}
}
>/msg Charlie This is a message for Charlie!!!
Sending message "This is a message for Charlie!!!" to "Charlie"...
>/msg
Unknown command or incorrect syntax
>/msg Charlie
Unknown command or incorrect syntax
This also assumes there's only 1 space between the /msg and the username, and the actual message. If you want whitespace between the tokens to not matter, there is a different solution.
#include <iostream>
#include <string>
#include <sstream>
///
/// Parses a command sent to see if it's a /msg command,
/// and if so, returns true and puts the username and message to be sent
/// into username_out and message_out, respectively.
///
bool parse_message_command(const std::string full_command, std::string& username_out, std::string& message_out)
{
std::istringstream iss(full_command);
std::string msg_token;
if (iss >> msg_token && msg_token == "/msg")
{
std::string username_token;
if (iss >> username_token)
{
std::string start_of_message;
// get first token of the actual message
if (iss >> start_of_message)
{
std::string rest_of_message;
getline(iss, rest_of_message);
message_out = start_of_message + rest_of_message;
username_out = username_token;
returntrue;
}
else
{
returnfalse;
}
}
else
{
returnfalse;
}
}
else
{
returnfalse;
}
}
int main()
{
// get the username and message from user input
std::string full_command;
std::cout << ">";
std::getline(std::cin, full_command);
// variables to store the username and message in
std::string username;
std::string message;
// get the username and message
if (parse_message_command(full_command, username, message))
{
// use the username and message
std::cout << "Sending message \"" << message << "\" to \"" << username << "\"..." << std::endl;
}
else
{
std::cout << "Unknown command or incorrect syntax" << std::endl;
}
}