please i need help with my code below, when ever i enter a wrond password first it gives me an error which its suppose to, but when entering the correct code next it still gives me an error message. could someone help me counter this problem. cheers

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

string Username;
string Password;
int terminator = 0, terminate = 0;

char encrypt;

while (terminate < 2)
{
cout << "Username: ";
getline(std::cin, Username);
if (Username == "Raymond") 
    
{
    terminate = terminate + 3;

    while (terminator < 2)
{
cout << "Password: ";
encrypt = _getch();


  while(encrypt != 13)

  { 
      Password.push_back(encrypt);
      cout << '*';
      encrypt = _getch();  
  }

if (Password == "123456")  
{
cout << "\n Welcome Raymond.... \n ";
terminator = terminator + 3;
}

else
{
cout << "invalid password. Please enter valid password." << std::endl;
cout << "\a\n";
terminator = terminator + 1;
}
}

} else {
cout << "invalid username. Please enter valid username." << std::endl;
cout << "\a\n";
terminate = terminate + 1;
}
} 

    
    return 0;
}

Add Password=""; before

while(encrypt != 13)

  { 
      Password.push_back(encrypt);
      cout << '*';
      encrypt = _getch();  
  }

That's because when the user enters a wrong password you keep Password with the wrong password and just add characters to it. You need to empty it before.

thankss very much dont no how too thank you. but i need a final solution to my problem of the backspce key inputtting as a character... please i would be very grateful if my request could be checked out. cheers

Try something like this instead of your while(encrypt!=13) loop:

while(encrypt != 13)
  {        
     if(encrypt!='\b') 
     {
     Password.push_back(encrypt);
     cout << '*';
     }
    else if(Password.length()>0) // Backspace only when possible
     {
     Password.resize(Password.length()-1); // Will also erase the last character
     cout<<"\b \b"; // Inserts a space in place of the previous asterisk and moves to the desired location
     }

  encrypt = _getch();
  }

thanks very much, but it still has a problem the backspace deletes a character, but cant delete a second and the program freezes. is there anything that can be done to resolve this? i really appreciate your responses. thank you so much, so sorry if am being persistent

thank you very much. your code came through for me.. you have really been of assistance to me..

This article has been dead for over six months. Start a new discussion instead.