This is just a simple menu and I have no clue why it doesn't work.

using namespace std;

#include <iostream>
#include <iomanip>
#include <fstream>

// ------ Prototypes ---------------------------------------------

void mainMenu();
void newEntry();
void display();
void exit();

// ****** MAIN ***************************************************
int main()


    return 0;

/* ------ MAIN MENU ----------------------------------------------
    1. Save a new password
    2. Display all passwords
    3. Exit
void mainMenu()
    int choice;

        cout << "** Invalid Entry **\n";
        cout << "Please choose 1, 2, or 3.\n";
        cout << "1. Save a new password\n";
        cout << "2. Display all passwords\n";
        cout << "3. Exit\n\n";
        cout << "Choose 1, 2, or 3: ";
        cin >> choice;
    }while(choice != 1 || choice != 2 || choice != 3);

        case 1: newEntry();
        case 2: display();
        case 3: exit();

void newEntry()
    cout << "New Entry";
void display()
    cout << "Display";
void exit()
    cout << "Exit";

Any integer including the valid 1,2,3 send error, and any non int sends it to infinite loop. This is absolutely basic and I'm beginning to wonder if I may have had a recent head injury because I can't for the life of me track down this MFing problem.

2 Years
Discussion Span
Last Post by Jjajangmyeon

while(choice != 1 || choice != 2 || choice != 3) is always true regardless of your input. The || operator will chain the operands and return true if any of them are true. Because choice can never be three values at once this statement will never be false and you will never break from the loop.

You probably want while(choice != 1 && choice != 2 && choice != 3) which will return false if only one of the operands is false. This will keep you in the loop so long as choice is not either 1, 2, or 3.


Thanks, but why is does it launch into an infite loop when any non int is entered?


Because your input stream goes into an error condition if you don't get integer input. Check for ! std::cin.good () on each iteration of the loop and do something if that check fails.

This is part of why handling arbitrary user input is so hard; you need to account for every possible situation.


The safer way to input an integer from cin is to use the following:

if(!(cin >> choice))

What it does is this. If the input operation failed (e.g., you did not enter a valid integer value), then it will put the stream in a failed state, which is what is tested with the if-statement here, to be interpreted as "if ( NOT ( stream in good state ) )". Then, it calls "clear()" on cin to clear the error state to make the stream readable again. Finally, in any case, it calls "ignore()" on cin such that any other thing that was inputted after the number itself is ignored, such that the next operation on the stream is not going to pick up those stray characters.

Without this, as you currently have it, if someone enters a non-numeric value, it reads nothing and puts the stream in an error state (std::ios_base::failbit) and all subsequent operations on it will have a similar effect, i.e., they will not read anything and just leave the stream in an error state. That's why you get into an infinite loop.


Ah! Got it! Thanks everyone! And Thank you for the thorough explanations, very informative!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.