如何键入名称并从加载的文件和结构数组中搜索人员信息? [英] how to type a name and search that persons info from file loaded into and array of structs?
问题描述
文件有1000行看起来像这样
114680858 19670607 Matilda Vincent MI
114930037 19471024 Desdemona Hanover ID
115550206 19790110 Xanadu Perlman ND
116520629 19630921 Alexander Hall SD
117050976 19301016 David Lamprey GA
119610646 19650202 Thomas Porlock IL
120330928 19621126 Cary Cartman NC
struct employees
{
int ss_number; //社会保障
int dob; //出生日期YYYY / MM / DD Ex。)19870314 = 1987/03/14
string f_name;
string l_name;
string state; //居住状态
};
void read_file() //将文件读入1000个结构的数组
{
ifstream data(/ home / www / class / een118 / labs / database1.txt);
员工阵列[1000]
if(!data.fail())
{
int i;
for(int i = 0; i< 1000; i ++)
{
data>> array [i] .ss_number
>> array [i] .dob
>> array [i] .f_name
>> array [i] .l_name
>> array [i] .state;
}
for(int i = 0; i< 1000; i ++)
{
cout<< array [i] .ss_number>><< array [i] .dob>><< array [i] .f_name>><<
array [i] .l_name>><< array [i] .state;
}
}
}
void print_person(员工e)
{
cout<< e.ss_number>>< < e.dob>><<< e.f_name>><< e.l_name>><< e.state;
} $ / $
无效搜索(employees array [])//键入名称并获取人员ss_number,dob等...
{
字符串优先;
字符串最后;
cout<<输入名称;
cin>> ;第一个>>最后一个;
for(int i = 0; i< 1000; i ++)
{
if(array [i ] .f_name == first&& array [i] .l_name == last)
{
print_person(array [i]);
}
}
}
无效主要()
{
员工array [10];
read_file();
search(array);
} s
file has 1000 lines that look like these
114680858 19670607 Matilda Vincent MI
114930037 19471024 Desdemona Hanover ID
115550206 19790110 Xanadu Perlman ND
116520629 19630921 Alexander Hall SD
117050976 19301016 David Lamprey GA
119610646 19650202 Thomas Porlock IL
120330928 19621126 Cary Cartman NC
struct employees
{
int ss_number;//social security
int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
string f_name;
string l_name;
string state; //state of residence
};
void read_file()//read file into array of 1000 structs
{
ifstream data("/home/www/class/een118/labs/database1.txt");
employees array[1000]
if(!data.fail())
{
int i;
for(int i=0;i<1000;i++)
{
data>>array[i].ss_number
>>array[i].dob
>>array[i].f_name
>>array[i].l_name
>>array[i].state;
}
for(int i=0;i<1000;i++)
{
cout<<array[i].ss_number>>" "<<array[i].dob>>" "<<array[i].f_name>>" "<<
array[i].l_name>>" "<<array[i].state;
}
}
}
void print_person(employees e)
{
cout<<e.ss_number>>" "<<e.dob>>" "<<e.f_name>>" "<<e.l_name>>" "<<e.state;
}
void search(employees array[])//type in name and get that persons ss_number,dob etc...
{
string first;
string last;
cout<<"Enter name";
cin>>first>>last;
for(int i=0;i<1000;i++)
{
if(array[i].f_name==first && array[i].l_name==last)
{
print_person(array[i]);
}
}
}
void main()
{
employees array[10];
read_file();
search(array);
}s
推荐答案
这篇关于如何键入名称并从加载的文件和结构数组中搜索人员信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!