0

I am writing a C++ authentication program for concept review and practice. I want to split the Data at Rest portion of the login credentials from the Data in Motion validity and specific username checks.

It seems std::map is the best way to do so before I add in hash and salting... but VS is refusing my attempts at passing references and elements even after initializing blank constructors and the like.

Below is my code from when I gave up after a week and folded the functions into one .h file... how do I split this into two?

{
    //create login Credential Map with prestored values
    std::map <string, string>CredentialMap = { {"robert", "password123"},{"wayne","password234"}, {"scott","password345"} };

    //ask for User's login 
    std::cout << "Enter Username" << endl;
    std::cin >> username;
    std::cout << "Enter Password" << endl;
    std::cin >> password;

    //check if valid login
    if (CredentialMap.find(username) != CredentialMap.end() && CredentialMap.at(username) == password) {
        return true;
    }
    else { return false; };
    }
pbgeek
  • 1
  • 3
  • What does the working code look like, what does the desired code look like, and what exact error are you getting? [tour] [example] – JohnFilleau Nov 04 '21 at 16:24
  • assuming this is contained in a function, `CredentialMap` is scoped to it and will die as soon as this function returns. You need to increase the scope of `CredentialMap` if you want it visible elsewhere. I would discourage giving raw access to the map. Rather, write a wrapper class around it and expose a public interface that allows users to work with it. Then in code elsewhere, you can create an object of that class. – yano Nov 04 '21 at 16:24
  • Possibly related: [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/) – Remy Lebeau Nov 04 '21 at 17:54

0 Answers0