输入一个值,然后将该值用作输入
input a value then use that value as input
我正在尝试获取一个输入,然后将输入值用于另一个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;
相关文章:
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 读取一组用户输入,按升序排序,然后打印结果
- 我将如何计算输入的所有商品的总和,然后计算总价?
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- Qt读取串行输入,然后将其拆分为单独的变量
- 有没有办法将 getline() 与外部文本文件一起使用,然后重新获得 cin 的控制权以从控制台获取输入?
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 流对象来表示文件输入,然后是标准输入?
- 如何存储多个用户输入,然后以C++显示它们
- 将int输入函数,将int输入开关,然后更改int的值
- 如果用户想要输入 x 数量的数字,我如何设法跟踪它然后结束程序?
- 如何声明一个未知大小的数组,然后输入直到我想要,然后获取数组的大小
- 使用ENUM输入和退出菜单,然后切换SFML
- 获取一个输入字符串,将其转换为其十六进制值,然后将其十六进制存储为 int
- 请求用户输入,然后忽略它
- 它可以工作,直到我输入第二个输入然后它关闭