首页 > 代码库 > C++ 显示星号密码

C++ 显示星号密码


#include <iostream>
#include <vector>
#include <conio.h>
using namespace std;

int main()
{
    vector< char > password;
    int len = 0;
    cout << "Enter password: ";
    while( true ){
        char ch = getch();
        if( ch == 13 )
            break;
        if( ch == 8 ){
            if( len == 0 )
                continue;
            len--;
            cout << "\b \b";
            password.pop_back();
        }
        else{
            len++;
            cout << '*';
            password.push_back( ch );
        }
    }
    cout << endl;
    for( int i = 0; i < password.size(); ++i ){
        cout << password[i];
    }
    return 0;
}