Skip to content

Commit b9a837d

Browse files
committed
code: handle errors in stoi
1 parent 5bec5ad commit b9a837d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ void read() {
6060
std::cout << ansi::CYAN << "Enter a number of seconds: " << ansi::RESET;
6161
std::string seconds;
6262
std::getline(std::cin, seconds);
63-
int seconds_int = std::stoi(seconds);
63+
int seconds_int;
64+
try {
65+
seconds_int = std::stoi(seconds);
66+
}
67+
catch (const std::invalid_argument& e) {
68+
std::cout << ansi::RED << e.what() << ansi::RESET << "\n";
69+
initialize();
70+
}
71+
catch (const std::out_of_range& e) {
72+
std::cout << ansi::RED << e.what() << ansi::RESET << "\n";
73+
initialize();
74+
}
6475
int timer = seconds_int;
6576
while (timer > 0) {
6677
std::cout << ansi::WHITE << timer << ansi::RESET << "\n";

0 commit comments

Comments
 (0)