C++에서 로그인 메뉴를 만들고 있습니다.RAW 모드에서 터미널을 사용하여 암호를 여러 번 입력하는 방법은 무엇입니까?
나는 프로그램을 종료하기 전에 사용자에게 암호를 3 회 입력하려고합니다.
사용자가 처음 암호를 올바르게 입력하면 내 코드가 올바르게 실행됩니다. 그런 다음 홈 메뉴 ect로 이동합니다. 그러나 사용자가 암호를 잘못 입력 한 경우 터미널은 다음과 같이 작동합니다.
Login: Fred
Password: ***
Wrong password
Please re-enter password:
사용자 유형에 상관없이 아무 것도 나타나지 않습니다. ctrl-C조차 프로그램을 종료 할 수 없습니다. 아무도 무슨 일이 일어나고 있는지, 그리고 올바른 방향으로 나를 지적 할 수 있는지 궁금해하고있었습니다.
여기에 "홈페이지"라는 클래스의 방법 "로그인"에 대한 코드의 일부 :
cout<<"Password: ";
while (loginAttempt < 3){ //The user gets to attempt to type
//the password 3 times
password = receivePassword(); //Receives password from user
if (flatMemberList[match].getPassword()==password){ //Check if the password is correct
cout<<endl<<"Welcome back "<<loginName<< endl; //If correct, display welcome message
return;
}
else{
loginAttempt++; //Record down one failed attempt
cout<<endl<<"Wrong password"<<endl; //If incorrect, display error
cout<<"Please re-enter password: ";
}
}
cout<<"you have exceeded the legal login attempts"<<endl;
exit(1);
다음과 같이 receivePassword()는 사용자 정의 방법 :
//This method is called when the user types in a password
//The terminal's setting is first changed to 'raw' configuration
//The password are taken in one letter at a time
//It outputs to terminal "*" instead of echoing the input
string HomePage::receivePassword(){
termios oldt, newt; //The structs for manipulation
char password[PaswordLength]; //Password held here
int j = 0; //Password index
tcgetattr(STDIN_FILENO, &oldt); //Get configuration details
newt = oldt;
cfmakeraw(&newt); //Set up new 'raw' configuration structure
//Set up new terminal configuration
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
cin.ignore(1000, '\n'); //flush all the buffers
while(true){
password[j] = cin.get();
if(password[j] == '\r') { //check if 'enter' key is entered
password[j] = '\0'; //replace cr with null to make C string
break;
}
cout.put('*'); //echo the asterisk
j++;
} ;
//Reset terminal to old configuration
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return password;
}
미리 감사드립니다.
문제가 다른 곳에서 발생할 수 있다고 생각되는 경우, 코드를 게시 할 것입니다.