0

I'm learning c++ on my own and don't have a teacher.

I've created a program that asks the user to enter their password over and over again until they enter the right password. It works, unless the user enters letters instead of integers. It just creates an infinite loop.

Could someone take a look at it and tell me what's wrong with it? Thank you!

#include <iostream>

using namespace std;

int main()
{
    int password= 123456;

    cout<< "Enter your password: "<< flush;
    int input;
    cin>> input;

    while(input!=password) {
        cout<< "Access denied."<< endl;
        cout<< "Enter your password: "<< flush;
        cin>> input;
    }

    cout<< "Access granted."<< endl;

    return 0;
}
CKE
  • 1,533
  • 19
  • 18
  • 29
phelps50
  • 1
  • 2
  • Hi, please refer to this question for clarification - https://stackoverflow.com/questions/46736705/c-input-validation-while-loop-not-terminating – arker296 Jun 20 '18 at 03:53
  • How do you expect the program to read letters into an integer variable? – eesiraed Jun 20 '18 at 04:48

0 Answers0