Hello. There is a task: "There are several users registered in the computer system. Develop a program that a user can determine whether they can access reading (r), write (w), or execute files (x) based on the user account name." I have a ready login and password verification code. But the problem is that it is only suitable for one login. How to put more logins here, and how to make different messages for different users?

#include <iostream> #include <string> using namespace std; int main() { string login = "Alice"; string password = "Bobly"; string log1; string pass; for (int i=0; i<3; i++) { cout << i+1 << "Enter login:"; cin >> log1; if (log1 == login) { cout << "Login accepted, enter password:" << endl; cin >> pass; if (pass == password) { cout << "Welcome!"; return 0; } else cout << "Access denied!"; return 0; } } } 
  • You want to implement an analog: sudo -u <username> [ -x <filename> ] ; echo $? sudo -u <username> [ -x <filename> ] ; echo $? ? - jfs
  • Not. If you enter the password correctly, it should be written what rights for the file are available to the user. By type: for the user Alice the rights rw rw rw are available, and for Bob - the rights rw r. In fact, after a successful login, Alice will write messages that such rights are available, and for Bob - such rights are available. - Boredix
  • one
    What do you think the team I brought does? - jfs
  • one
    Do you want to do this for some real OS (for example, Linux) or for your own access model? If for your own, then describe it in more detail - avp
  • Something like that. For your own access model, of course. Those. like the team you wrote. - Boredix

2 answers 2

Here is the option of checking multiple accounts. If you had to correct the code in question, you can. And if you are interested in the command in the terminal to determine the rights of users, then sudo ... to help you (see comment jfs).

 #include <iostream> #include <iterator> #include <utility> #include <cstdint> #include <cstdlib> #include <string> #include <map> using index_t = std::uint64_t; int main(int argc, char* argv[]) { const index_t N = 3; // Number of attempts std::map< std::string, std::pair< std::string, std::string > > m; m.emplace(std::make_pair(std::string("Alice"), std::make_pair(std::string("1234"), std::string("Hello, Alice! You can: [rw, rw, rw]")))); m.emplace(std::make_pair(std::string("Bob"), std::make_pair(std::string("5678"), std::string("Hello, Bob! You can: [r, r, r]")))); std::string login, password; for (index_t i = 0; i < N; ++i) { std::cout << "Enter login #" << i + 1 << ": "; std::cin >> login; auto login_iterator = m.find(login); if (login_iterator != std::end(m)) { std::cout << "Enter password: "; std::cin >> password; if (login_iterator->second.first == password) std::cout << login_iterator->second.second << std::endl; else std::cout << "Error: wrong password." << std::endl; } else std::cout << "Error: login \"" << login << "\" not found." << std::endl; } system("pause"); return EXIT_SUCCESS; } 

If the user needs to store additional information, besides the password and the line with the list of rights, then the container element should be replaced with the pair with the user structure and refer to the fields of this structure ...

  • A person came to you with a 2+2 problem, and you rub him about integrals :) - ffk
  • @ffk Chicks learn to fly falling from the nest. - Jenssen

Use the default vector (A. Alexandrescu)

Create an array of logins, and even better, an associative container map , in which you store the login as the key, and the value of the std::pair as the key:

{password; individual message for login}.

When entering a login, use the standard function of this associative container find(key) , which returns an iterator to the corresponding element in the container with the key specified as an argument, and the end iterator if the key is not present. You can read about this feature, for example, here . A description of the map container can be found here .

Further, you have an element of the container - the above pair. You get access to the field with the password through .first , compare, get access to the field with the message through .second . You can read the description of the pair here, for example