The line "getline(cin,get_password);" doesn't repeat if the user chooses to re-enter the password. It pauses for a short while when it should run that line, refuses to accept any data, and then continues as normal.

#include<iostream>
#include<string>
#include<windows.h>
using namespace std;

int main()
{
    string get_password;
    string password="abc4";
    char repeat1;
    char repeat;
    do
    {

        cout<<"Please enter the password"<<endl;//The password is abc4
        getline(cin,get_password);
        do
        {
            cout<<"Password length check...";
            Sleep(1000);
            if(get_password.length()==password.length())
            {
                cout<<"Completed"<<endl;
                Sleep(500);
            }
            else
            {
                cout<<"Incorrect password"<<endl;
            }
        }
         while(repeat1=='Y'||repeat1=='y');

        if(get_password==password)
        {
            cout<<"The password is correct."<<endl;
            repeat='n';
        }
        else
        {
            cout<<"The password is incorrect."<<endl;
            cout<<"Do you want to re-enter the password?\tY/N"<<endl;
            cin>>repeat;

        }
    }
    while(repeat=='Y'||repeat=='y');
    return 0;



}

Recommended Answers

All 4 Replies

Yea the problem is that when the user enters 'y' or 'Y', he is actually entering that plus a newline character. The getline reads the newline character as a valid input, thus it gives you no opportunity to input a second time. One solution is to use cin.ignore() before the getline so that it ignores the newline character.

Alright thanks. It works perfectly now.

Also, one of your do-while s is testing repeat1, but the variable is never set nor read. It contains garbage.

I noticed it, but just wanted to clean up after I got the problem out of the way. It's out now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.