C++ help, objects / variable names

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
I am trying to google my issue, but I think I don't know the right terminology

I am making a "login with username and password" toy program

I have some objects in a class, and I want to compare "username" to "password"

I am trying to write the code so that when it runs it will identify when a username matches a password, not just one username and password combination...but from all of them at once

I am brand new to C++ so please be gentle!!! I just don't know the terms to enter into a search

Code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>


using namespace std;


class User
{
public:
    string username;
    string password;
   
    string key;
    string pass;
};

int main ()
{
    string key;
    string pass;

       
    User user1;
    user1.username = "Mark";
    user1.password = "Jambalaya";
   
    User user2;
    user2.username = "Grace";
    user2.password = "Gumbo";
   
    User user3;
    user3.username = "Addie";
    user3.password = "Shrimp";
   
    User user4;
    user4.username = "Natalie";
    user4.password = "Spices";
   
    User user5;
    user5.username = "Hillary";
    user5.password = "Sausage";

   
    std::cout << ("Enter your username:\n") << endl;
    std::cin >> key;
   
    std::cout << ("Now, enter your password:\n") << endl;
    std::cin >> pass;
   
    if (key == User.username && pass == User.password) //THIS IS WHERE I CANT FIGURE OUT THE RIGHT SYNTAX
    {
        std::cout << ("Access granted!\n") << endl;
    }
    else
        std::cout << ("Access denied!\n") << endl;
   
}
 

Art Vandelay

Joined Nov 1, 2024
140
One way is to store the accounts and passwords in an unordered map. The map is stored as key:value pairs and each key is unique. The if statement uses the find() method to locate the key then compares it to the corresponding password. This example doesn't have any encryption, error handling or input sanitation so it's for demonstration only.

C++:
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

int main() {
    
    unordered_map<string, string> accounts = {
        {"Tom", "123"},
        {"Jeff", "456"},
        {"Sara", "789"}
    };

    string username, password;

    cout << "Welcome to the login system!" << endl;
    cout << "Please enter your username: ";
    cin >> username;

    cout << "Please enter your password: ";
    cin >> password;

    if (accounts.find(username) != accounts.end() && accounts[username] == password) {
        cout << "Login successful! Welcome, " << username << "!" << endl;
    } else {
        cout << "Login failed. Invalid username or password." << endl;
    }

    return 0;
}
 

BobTPH

Joined Jun 5, 2013
11,503
Although the solution give above is perfectly OK, it is “cheating” by using a library function that hides how all of the work is done.

If your goal is to learn programming, I would suggest that you think about how you would do it without such a library function.

A computer does not do anything like that “all at once.” It must use a loop to find if any of the usernames matches.

What do you know about dara structures? Are you familiar with arrays? Linked lists? Trees? There are many ways to do this. In fact, you could make quite a good programming text based in more and more efficient programs for this task. Think Amazon. They must have quite an algorithm to verify one of a billion users quickly.

The simplest way would be to use an array of User objects, then loop through them, one at a time, checking to see if the name and password match.
 

Art Vandelay

Joined Nov 1, 2024
140
This code uses two arrays to store the usernames and passwords. The for statement loops through each item in the array checking two things:

C++:
strcmp(inputUsername, usernames[i]) == 0 // Returns true if inputUsername = usernames[i]
strcmp(inputPassword, passwords[i]) == 0 // Returns true if inputPasswords = passwords[i]
The && (and operator) requires both of these conditions to be true at location i in each array. In other words, the loop checks the arrays in parallel but must still loop through the entire array at least once in the worst case. The best case is the username and password are both at location [0] in the array. Specifying the index i in each array ensures the arrays are compared in sequential order to prevent accidental mismatches.

C++:
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    const char* usernames[] = {"Tom", "Jeff", "Sara"};
    const char* passwords[] = {"123", "456", "789"};
    int numUsers = 3;
    char inputUsername[50];
    char inputPassword[50];
    bool loginSuccess = false;

    cout << "Please enter your username: ";
    cin >> inputUsername;
    cout << "Please enter your password: ";
    cin >> inputPassword;

    for (int i = 0; i < numUsers; i++) {
        if (strcmp(inputUsername, usernames[i]) == 0 && strcmp(inputPassword, passwords[i]) == 0) {
            loginSuccess = true;
            break;
        }
    }

    if (loginSuccess) {
        cout << "Login successful!" << endl;
    } else {
        cout << "Login failed." << endl;
    }
 
    return 0;
}
 
Last edited:

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
Although the solution give above is perfectly OK, it is “cheating” by using a library function that hides how all of the work is done.

If your goal is to learn programming, I would suggest that you think about how you would do it without such a library function.

A computer does not do anything like that “all at once.” It must use a loop to find if any of the usernames matches.

What do you know about dara structures? Are you familiar with arrays? Linked lists? Trees? There are many ways to do this. In fact, you could make quite a good programming text based in more and more efficient programs for this task. Think Amazon. They must have quite an algorithm to verify one of a billion users quickly.

The simplest way would be to use an array of User objects, then loop through them, one at a time, checking to see if the name and password match.
Thanks! I made an array , and then I was able to scrounge up some code from the internet for the part I couldn't figure out at first:

Code:
if (key == array[i].username && pass == array[i].password)
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
It seems odd that I can't call the attribute of a function by matching a string (attribute) to another string from standard input...and that I would have to use an array

EX: I have a list of objects (students) with names and "grades" (like the grades you get in school). I am trying to perform a calculation based on attributes of these objects, all from the same class...When I type a student's name that's listed, the program would continue to calculating GPA based on a bunch of factors. I find it odd that there would be no way of doing this besides making an array

Am I wrong? is there a way?

Again I'm brand new to C++ so please be gentle
 
Top