输入一个值,然后将该值用作输入

input a value then use that value as input

本文关键字:输入 然后 一个      更新时间:2023-10-16

我正在尝试获取一个输入,然后将输入值用于另一个cin值。这些代码无法编译(预期),只是为了说明我的想法:

class StaffLogin : public ShareData {
private:
    string ID;
    void authorized()
    {
        struct staffs
        {
            string username;
            string password;
        };
        staffs id700014089, id700014090;
        id700014089.username="Robin";
        id700014089.password="c++ is fun";
        cin>>ID;
        cout<<"Username: ";
        cin>>"ID".username;
        cout<<"Password: ";
        cin>>"ID".password;
    }
};

例如,我想从用户那里获取一个 ID,所以cin>>ID.然后在另一个输入(cin>>"the ID from previous cin".username)中使用该值,以便我可以轻松地为新用户创建新的ID,用户名和密码。请告诉我是否有方法可以做到这一点?

编辑:好的,我通过使用 map<string,string> stafflist; map<string,string>::iterator it; 没有任何结构。请发表评论,以防你们中的一些人需要更多详细信息。;)

C++不

是一种动态语言,因此要使用用户输入提供的名称动态创建对象,您需要使用某种键值容器,允许您将任意名称与对象相关联。C++中的标准解决方案是std::map<Key, Value>std::unordered_map<Key, Value>

假设员工 ID 始终是数字,您可以这样做:

struct staffs
{
  string username;
  string password;
};
using StaffDirectory = std::map<unsigned long, staffs>;
StaffDirectory staff_directory;
// ...
unsigned long id;
if (std::cin >> id)  // read the ID
{
  // check the 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 and password
  {
    // add the object to the map, using `id` as the key
    staff_directory[id] = s;
  }
}

你所要求的是不可能的。但是,您可以这样做:

struct staffs {
    string ID;
    string username;
    string password;
};
staffs s;
cin>>s.ID;
cout<<"Username: ";
cin>>s.username;
cout<<"Password: ";
cin>>s.password;

或者,根据您要对此数据执行的操作,您还可以使用std::map<string,staffs>在 ID(第一个模板参数)和用户数据(存储在 staffs 中)之间建立显式映射。

编辑:在阅读您的评论之前,我并不清楚您想做什么。与此同时,乔纳森·韦克利(Jonathan Wakely)已经给出了一个更详细的使用地图的例子。

#include <iostream>
#include <map>
#include <string>
using namespace std;
struct Staff
{
    string username;
    string password;
};
int main()
{
    map<string, Staff> _map;
    while (true)
    {
        string id;
        Staff staff;
        cout << "Enter id: ";
        cin >> id;
        bool idExists = true; //assume id exists
        if (idExists)
        {
            cout << id << ": username: ";
            cin >> staff.username;
            cout << id << ": password: ";
            cin >> staff.password;
            _map.insert(_map.begin(), pair<string, Staff>(id, staff));
        }
        int choice;
        cout << "nAdd? {1 or 0}: ";
        cin >> choice;
        cout << endl;
        if (choice != 1)
            break;
    }
    for (auto it = _map.begin(); it != _map.end(); it++)
        cout << "n Id: " << it->first << ": Username :" << it->second.username << " , Password: " << it->second.password << endl;
   return 0;
}

你不这样做ID因为它不是结构器对象。如果要使用结构变量,首先必须创建结构体对象。

例:

staffs st;  // **create staffs structer object**
cin>>st.ID;
cout<<"Username: ";
cin>>st.username;
cout<<"Password: ";
cin>>st.password;
相关文章: