c++ - input a value then use that value as input -
i trying input , use input value cin
value. these codes cannot compiled (expected), illustrate idea:
class stafflogin : public sharedata { private: string id; void authorized() { struct staffs { string username; string password; }; staffs id700014089, id700014090; id700014089.username="robin"; id700014089.password="c++ fun"; cin>>id; cout<<"username: "; cin>>"id".username; cout<<"password: "; cin>>"id".password; } };
for example, want id user, cin>>id
. use value in input (cin>>"the id previous cin".username
) can create new id, username , password new user. please tell me if there method it?
edit: ok found simpler solution using map<string,string> stafflist; map<string,string>::iterator it;
without struct. comment in case of need more details. ;)
c++ not dynamic language, create objects dynamically names supplied user input need use kind of key-value container let's associate arbitrary names objects. standard solutions in c++ std::map<key, value>
or std::unordered_map<key, value>
.
assuming staff ids numbers this:
struct staffs { string username; string password; }; using staffdirectory = std::map<unsigned long, staffs>; staffdirectory staff_directory; // ... unsigned long id; if (std::cin >> id) // read id { // check id if (!validatestaffid(id)) throw std::runtime_error("invalid staff id: " + to_string(id)); staffs s; if (std::cin >> s.username >> s.password) // read username , password { // add object map, using `id` key staff_directory[id] = s; } }
Comments
Post a Comment