A short record such as "room:west" contains two pieces separated by a colon. find locates text, and substr copies part of a string into a new string.
#include <iostream>
#include <string>
#include <cstddef>
int main() {
std::string record = "room:west";
std::size_t separator = record.find(':');
if (separator != std::string::npos) {
std::string side = record.substr(separator + 1);
std::cout << side << '\n';
}
return 0;
}
find(':') returns the index of the first colon. If no match exists, it returns std::string::npos, a special “not found” value. Do not treat that value as an ordinary character position. Check it before using the position in arithmetic or a slice.
substr(start) copies from start to the end. Here separator + 1 starts just after the colon. A second argument can limit the number of characters: record.substr(0, 4) copies four characters starting at 0. It is a count, not an ending index. A start equal to the string's size gives empty text; keep starts within the valid range.
Start in main, where separator currently holds the not-found marker rather than a search result. Replace that initializer and leave the following check in place. Then move up to afterSeparator and replace its empty-string return. This helper assumes its position identifies a separator in the text. Its supplied call passes the known position 4, so it can work even while the search task is unfinished. For text you have not checked, use the guarded search pattern shown above before calling the helper.
separator using record.find(':'). Keep the supplied not-found check. Expect Separator: 4.afterSeparator return the substring beginning one position after its valid position parameter. Expect Name: Garden.Edit main.cpp, press Run to inspect the Console, then Submit to check your work.