4.30 编写一个程序,已有若干用户的用户名和密码,密码输入时不能显示输入结果,记录用户个数,最后将信息输出。(用静态数据成员表示用户的个数)

4.30 编写一个程序,已有若干用户的用户名和密码,密码输入时不能显示输入结果,记录用户个数,最后将信息输出。(用静态数据成员表示用户的个数)。

#include<iostream>
#include<vector>
#include<conio.h>

using namespace std;

class User {
private:
    string username;
    string password;
    static int userCount;

public:
    User(string uName, string pwd) {
        username = uName;
        password = pwd;
        userCount++;
    }

    void display() {
        cout << "Username: " << username << endl;
        cout << "Password: ********" << endl;
    }

    static int countUsers() {
        return userCount;
    }
};

int User::userCount = 0;

int main() {
    vector<User> users;
    char choice = 'y';
    string uName, pwd;


    while (choice == 'y' || choice == 'Y') {
        cout << "Enter username: ";
        cin >> uName;
        cout << "Enter password: ";
        char c = _getch();
        pwd = ""; 
        while (c != 13) {
            pwd.push_back(c);
            cout << "*";
            c = _getch();
        }
        cout << endl;
        users.push_back(User(uName, pwd));
        cout << "Do you want to add more users?(y/n): ";
        cin >> choice;
    }
    cout << "\n\nUser Details: \n" << endl;
    for (int i = 0; i < users.size(); i++) {
        cout << "User " << i + 1 << ": " << endl;
        users[i].display();
        cout << endl;
    }

    cout << "\nTotal number of users: " << User::countUsers() << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值