简体   繁体   English

任何可能将多个名称添加到变量 c++

[英]Any possible to add more than one name into a variable c++

i am trying to make a login system that uses the user from the PC.我正在尝试制作一个使用 PC 用户的登录系统。 The problem is that i want to add more than one user.问题是我想添加多个用户。 Thanks in advance.提前致谢。

THIS CODE ACTUALLY WORKS!!此代码实际上有效!

using namespace std;
string user = "LaCk";

int main()
{
    SetConsoleTitle("checking whitelist...");
    std::cout << "checking whitelist...\n";

    TCHAR username[UNLEN + 1];
    DWORD usernamel_len = UNLEN + 1;

    GetUserName((TCHAR*)username, &usernamel_len);

    if (username == user)
    {
        SetConsoleTitle("Welcome");
        std::cout << "Welcome back "; wcout << username << endl;
        Sleep(500);
    }
    else
    {
        SetConsoleTitle("YOU ARENT IN THE WHITELIST");
        Sleep(15000);
    }

}

Change your user variable from a single std::string to a std::vector of std::string elements.将您的user变量从单个std::string更改为std::string元素的std::vector Then you can store multiple strings, and can use std::find() to search it.然后你可以存储多个字符串,并且可以使用std::find()来搜索它。

Also, you don't need the (TCHAR*) type-cast, since your username array will decay into a TCHAR* for you.此外,您不需要(TCHAR*)类型转换,因为您的username数组将为您衰减TCHAR* However, you should be using GetUserNameA() instead, since you are dealing with char data.但是,您应该改用GetUserNameA() ,因为您正在处理char数据。 Don't use TCHAR at all in modern coding.在现代编码中根本不要使用TCHAR

Try this:尝试这个:

#include <vector>
#include <string>
#include <algorithm>

std::vector<std::string> users;

void loadUsers()
{
    users.push_back("LaCk");
}

std::string getCurrentUser()
{
    std::string res;

    char username[UNLEN + 1];
    DWORD usernamel_len = UNLEN + 1;

    if (GetUserNameA(username, &usernamel_len))
        res.assign(username, usernamel_len-1);

    return res;
}

int main()
{
    loadUsers();

    SetConsoleTitle("checking whitelist...");
    std::cout << "checking whitelist...\n";

    std::string username = getCurrentUser();

    if (std::find(users.begin(), users.end(), username) != users.end())
    {
        SetConsoleTitle("Welcome");
        std::cout << "Welcome back " << username << std::endl;
        Sleep(500);
    }
    else
    {
        SetConsoleTitle("YOU ARENT IN THE WHITELIST");
        std::cout << "Sorry " << username << ", access denied!" << std::endl;
        Sleep(15000);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM