diff --git a/cpp/robot-name/robot_name.cpp b/cpp/robot-name/robot_name.cpp index 5b79010..1a06dcf 100644 --- a/cpp/robot-name/robot_name.cpp +++ b/cpp/robot-name/robot_name.cpp @@ -1,6 +1,7 @@ #include "robot_name.h" #include +#include #include #include @@ -49,6 +50,9 @@ void robot::reset() static distribution_t alpha_distribution('A', 'Z'); static distribution_t digit_distribution('0', '9'); + if (used_names.size() >= name_capacity) + throw length_error("no more names available"); + string new_name; do { @@ -56,7 +60,7 @@ void robot::reset() new_name = random_string(alpha_part_size, alpha_distribution) + random_string(digit_part_size, digit_distribution); - } while (used_names.size() < name_capacity && used_names.count(new_name)); + } while (used_names.count(new_name)); used_names.erase(name_); name_ = new_name;