Home > Net >  What am I missing for my input validation to execute properly?
What am I missing for my input validation to execute properly?

Time:07-17

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?

enter image description here

#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;

}

CodePudding user response:

Indeed, you cannot do anything with a empty string. You may have thought that you have already created a string south; and the value of string south; is south. But you are wrong. Like @Sam Varshavchik quoted:

The Golden Rule Of Computer Programming states: "your computer always does exactly what you tell it to do instead of what you want it to do".

And you told the computer that I want to declare a variable named south with a empty string.

For your information, the syntax of just declaring a variable is:

type variableName;

The syntax of declaring a variable value is:

type variableName = value;

In your case:

string south = "south";

And you continue on adding the value for the variables.


The sample code:

#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 = "north";
    string south = "south";
    string east = "east";
    string west = "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;

}

CodePudding user response:

    string north;
    string south;
    string east;
    string west;

These lines of code declare four std::string objects. By default, all four of them are completely empty. Nothing in the code that follows changes them, so they'll remain empty.

        while (direction != north && direction != south &&
               direction != east && direction != west)

The Golden Rule Of Computer Programming states: "your computer always does exactly what you tell it to do instead of what you want it to do". So, let's figure out what this line of code tells your computer to do.

This line of code tells your computer to execute what's in the while loop as long as the direction string, what's typed in, is different from all four of these std::string objects.

All four of these std::string objects are exactly the same: they're empty.

So, as long as direction is not empty, this while loop will execute. direction is what's typed in, and read from std::cin, and it cannot be empty. So, direction is never empty, and, therefore, will always meet the conditions of the while loop.

This is what you told your computer to do. And that's what you've observed your computer doing: you enter a non-empty string, and since it's not empty, it always meets the condition for the while loop to execute.

If you want your computer to do something different, simply change the above logic, accordingly, keeping in mind The Golden Rule. If you want to recognize single letters, as the possible directions ("n", "s", "e", "w"), then set those four std::strings accordingly. Or, set them to complete words, if you want your computer to recognize only complete words ("north", "south", "east", and "west").

  • Related