From e1d89f3467a0e1410524ad882fb243b4231de4df Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Mon, 7 Mar 2016 20:42:14 +0300 Subject: [PATCH] robot_name: iteration 4; throw an exception --- cpp/robot-name/robot_name.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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;