在线提问 在线寻求答案解决一下啦C++

img

描述
 Alice在TZOJ申请了一个账号,账号名为“admin”,账号密码为“Python@16”。现在给你她输入的账号密码,请问他是否可
 以登录上他的账号。
输入
  第一行为输入的账号,长度不超过20字符, 不包含空格
 第二行为输入的密码,长度不超过20字符,不包含空格
输出
 若账号密码均正确,请输出“Loginsuccessful,若有误请输出“Usemameorpasswordiswrong
样例输入
  admin
 Pythone16
样例输出
 Login successful
提示
 样例输入2
  admin
  Python@l6
  样例输出2
  Username or password is wrong

C++中,string的变量是否相等是可以直接用==比较的,
代码如下:

void password_check()
{
    string number = "admin";
    string password = "Python@16";
    
    string num = "";
    string pass = "";
    cout << "输入账号" << endl;
    cin >> num ;
    cout << "输入密码" << endl;
    cin >> pass ;

    if((num == number) && (pass == password))
        cout << "Login successful" << endl;
    else
        cout << "Username or password is wrong" << endl;
}

供参考:

#include<string.h>
#include<stdio.h>
struct user
{
    char name[20];
    char password[20];
}users[5] = {{"admin","Python@16"}};

int main()
{
    int  j;
    char name[20], password[20];
    scanf("%s", name);
    scanf("%s", password);
    for (j = 0; j < 5; j++){
        if (strcmp(users[j].name, name) == 0 && strcmp(users[j].password, password) == 0)
        {
            printf("Login successful");
            break;
        }
    }
    if (j >= 5)
        printf("Username or password is wrong");
    return 0;
}