Skip to content

Commit 681f9f0

Browse files
committed
Adding checking if input is digit
1 parent d4fc6e0 commit 681f9f0

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

shm/Game.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void Game::travel()
7272
printMap(*map_);
7373
std::cout << "Choose Your destination captain!" << '\n';
7474
std::cin >> i;
75-
if (i < map_->islands_.size() && i >= 0)
75+
if (isdigit(i) && i < map_->islands_.size() && i >= 0)
7676
{
7777
auto travelTime = map_->calculateDistance(map_->islands_.at(i)) / playerOne_->getSpeed();
7878
std::cout << "Your travel will take " << travelTime << " days." << '\n';
@@ -84,6 +84,8 @@ void Game::travel()
8484
else
8585
{
8686
std::cout << "There is no such island on the map! Choose another destination!" << '\n';
87+
std::cin.clear();
88+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
8789
Game::travel();
8890
}
8991
}

0 commit comments

Comments
 (0)