In this function when i try to write in invalid command like "arblegarble" it hits the else if statement and stays in the loop. However, When i purposely go in any valid direction (North, South, East, or West), it prints out correctly when there is a wall or some other kind of issue, but never stays in the loop. I feel like somehow my do while loop is broken but for the life of me I can't figure out why it isn't working properly.
std::string isValidDirection (int coordinateX, int coordinateY){
bool isValid;
std::string direction;
//This loop validates if your direction/exit is a valid
//direction/exit and if you typed a valid direction/exit name.
do {
isValid = true;
std::getline(std::cin, direction);
if ((!gameField[coordinateX + 1][coordinateY] || !gameField[coordinateX][coordinateY + 1]
|| !gameField[coordinateX][coordinateY - 1] || !gameField[coordinateX - 1][coordinateY])
&& (direction == "North" || direction == "South" || direction == "East" || direction == "West")) {
std::cout << "You can't go that way. Please try a different way.\n";
isValid = false;
}
elseif (direction != "North" && direction != "South" && direction != "East" && direction != "West") {
std::cout << "That is not a valid Direction. Please Try again.\n";
isValid = false;
}
} while (!isValid);
return (direction);
}
while (!isValid);
This is saying, run the loop as long as it is not true, meaning false. You want it to run as long as it is true, and then once you enter an invalid statement like "arblegarble", you want it to hit the ifelse statement, become false and quit the loop.
What I want to have happen is that the direction is valid when the program exits or escapes the loop and only when that happens.
The problem was that when it hits the if statement on line 10: it prints out the error message "You can't go that way. Please try a different way." but then seems to miss the isValid being set to false which if it did seems like it would stay in the do while loop. However when it hits the else if statement then it works the way i intended.
My question and struggle is that why is that happening?
Is there some part of my code that is not functioning the way i think it should?
The way you have it right now the loop is inifinite. Either you type North, South, East or West, and set isValid to false. Or you type anything else and set isValid to false.
Your loop runs as long as isValid is false. It will always be false, because no matter what we type in, it will be picked up by one of the if statements and be set to false.
What exactly do you want this function to do? When do you want it to end? What does the user have to enter for it to end?
The way you have it right now the loop is inifinite. Either you type North, South, East or West, and set isValid to false. Or you type anything else and set isValid to false.
Oh, I thought that reseting the isValid to true would do fix this on line 8, and you don't realize that the gamefield is a multi-dimensional boolean typed array with some places in the array with false to signify that the player cannot go that way, like so: bool gameField[4][3] = { true, false, true, true, true, true, false, true, true, false, false, true };
TarikNeaj wrote:
What exactly do you want this function to do? When do you want it to end? What does the user have to enter for it to end?
What I want the function to do is to verify that moving east is a valid move and that there isn't a wall or door in the way. Maybe I should implement a map for the player/user to see where they can go. Ideally the user/player would have to type North, South, East, or West and that direction/exit is not obstructed (saying that the gameField will not resolve to false in that direction.).
So basically, if your if statement comes true, You want the player to be able to re-answer. But if your else-if statement comes true, you want to exit the loop?
So Basically, I want to check if the direction of the player/user wishes to go in the game field is valid meaning that if the game field does not show that the user is unable to move that direction, then the point in that direction is false.
Maybe i should rewrite the code to the following to make what I want easier to see:
do {
//reset the user flag to true.
isValid = true;
//prompt the user for direction.
std::getline(std::cin, direction);
//this verifies that the direction and the point in the multi-dimensional array in
//that direction is marked as false meaning that the user cannot go that way.
//allow the user to reenter the direction they want to go.
if ((!gameField[coordinateX + 1][coordinateY] && direction == "South" )
|| (!gameField[coordinateX][coordinateY + 1] && direction == "East")
|| (!gameField[coordinateX][coordinateY - 1] && direction == "North")
|| (!gameField[coordinateX - 1][coordinateY] && direction == "West") {
std::cout << "You can't go that way. Please try a different way.\n";
isValid = false;
}
//Otherwise if the direction is not a valid direction then allow the user to reenter it.
elseif (direction != "North" && direction != "South" && direction != "East"
&& direction != "West") {
std::cout << "That is not a valid Direction. Please Try again.\n";
isValid = false;
}
} while (!isValid);
If this fails to make sense please let me know how it doesn't make sense and I will do my best to help make it make sense.
Your problem is simple, You have no condition/ifstatement that sets isValid to true to exit the loop, so it runs forever. You need an if statement which sets isValid to true if the user enters the correct thing.
1 2 3 4 5 6 7 8
if(user entered wrong direction)
{
isValid = false; // to reenter
}
elseif(user enter a valid choice)
{
isValid = true; // Loop exits because the user enter a choice that works
}
Only you know what the user has to write to get his direction valid.