2011-10-22 3 views
0

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; 
    } 

미리 감사드립니다.

문제가 다른 곳에서 발생할 수 있다고 생각되는 경우, 코드를 게시 할 것입니다.

답변

0

귀하의 구체적인 문제가 무엇인지 모르겠습니다. 그러나 표준 디버깅 기술을 적용하여 문제의 원인이되는 부분을 찾을 수 있습니다.

먼저, 터미널 (cfmakeraw, tcsetattr 등)으로 이상한 일을하고 있습니다. 그게 문제와 관련이있는 것 같습니다. 따라서 은 사용자 입력을 숨기는 코드 인을 제거하고 암호가 화면에 정상적으로 울릴 때 프로그램이 작동하는지 확인하십시오. 당신은 그렇게 쉽게 할 수 있어야합니다. 당신이 제거

  • 터미널 속성 코드 또는 유지
  • 암호 루프 :

    이 일 후에는 문제가 관련이 있는지 여부를 결정할 수 있습니다.

이것은 종종 "분할 및 정복"디버깅 기술이라고합니다. 코드를 제거하면을 문제라고 생각하면 문제가 남아 있는지 여부에 관계없이 제거한 코드와 관련 있는지 여부를 결정하는 데 도움이됩니다.

관련 문제