I am trying to use input validation with a while loop to ensure user cannot enter anything for a direction that is not north, south, east, or west. I am able to compile without error but currently I cannot enter anything in direction that doesn't yield my "Invalid entry" statement".
What am I doing wrong?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
cout << "********************Welcome to the COSC-1436*******************\n";
cout << "******************Version of the Classic Text Game*****************\n";
cout << "*****************************Zork*****************************\n";
double kilometers = 25;
double miles = kilometers * 0.62137;
cout << "***************This map covers " << kilometers << " km" << "/" << setprecision(7) << miles << " miles***************" << endl;
string direction; // direction user would like to move.
double steps; // user's number of steps.
const double STEPS_PER_MILE = 2000; // steps per mile.
const int STEPS_TO_FOREST = 50; // number of steps to the forest.
const int STEPS_TO_DUNGEON = 100; // number of steps to the dungeon.
const int STEPS_TO_HOUSE = 4000; // number of steps to the house.
const int STEPS_TO_MAZE = 6000; // number of steps to the maze.
string north;
string south;
string east;
string west;
int menuChoice;
//Ask user what action they would like to take and display menu.
cout << "\nWhich action would you like to take?\n\nPlease choose a number from the menu options below and then click 'enter':\n\n";
cout << "1. Move\n";
cout << "2. Attack\n";
cout << "3. Pick up Item\n";
cout << "4. Quit\n";
cin >> menuChoice;
switch (menuChoice)
{
case 1: cout << "You chose option 1. Move\n";
{
cout << "What direction would you like to move?\n"; //Get the direction user would like move
cin >> direction;
while (direction != north && direction != south && direction != east && direction != west)
{
cout << "Invalid entry! "
<< "Please enter a direction that is either north, south, east, or west.\n";
cin >> direction;
}
cout << "How many steps are you taking?\n"; //Get the number of steps user is taking.
cin >> steps;
while (steps < 0)
{
cout << "Invalid entry!"
<< " Please enter a number greater than 0.\n";
cin >> steps;
}
double milesMoved = steps / STEPS_PER_MILE;
double kilometersMoved = milesMoved / 0.62137;
//Determine and display result.
cout << "\nYou walked " << milesMoved << " miles and " << kilometersMoved << " kilometers " << direction << ".\n";
//If the number of steps is greater than or equal to the number of steps to reach the forest, and the user is moving north.
if (steps >= STEPS_TO_FOREST && direction == "north")
cout << "\nYou have now entered the forest.\n";
//If the number of steps is greater than or equal to the number of steps to reach the dungeon, and the user is moving south.
else if (steps >= STEPS_TO_DUNGEON && direction == "south")
cout << "\nYou have now entered the dungeon.\n";
//If the number of steps is greater than or equal to the number of steps to reach the house, and the user is moving east.
else if (steps >= STEPS_TO_HOUSE && direction == "east")
cout << "\nYou have now entered the house.\n";
//If the number of steps is greater than or equal to the number of steps to reach the maze, and the user is moving west.
else if (steps >= STEPS_TO_MAZE && direction == "west")
cout << "\nYou have now entered the maze.\n";
else
cout << "\nYou are now lost.\n";
}
break;
case 2: cout << "You chose option 2. Attack\n";
break;
case 3: cout << "You chose to pick up item\n";
break;
case 4: cout << "You chose to quit\n";
return 0;
break;
default: cout << "You did not enter 1, 2, 3, or 4 from the menu choices provided\n";
}
system("pause");
return 0;
}
