【发布时间】:2014-04-17 22:18:07
【问题描述】:
使用此程序,当我输入名称时,不会返回任何内容。
我该如何解决这个问题?
有 1000 行信息如下所示:
114680858 19670607 玛蒂尔达文森特 MI
114930037 19471024 Desdemona Hanover ID
115550206 19790110 Xanadu Perlman ND
116520629 19630921 亚历山大大厅 SD
117050976 19301016 大卫·兰普雷 GA
119610646 19650202 托马斯·波洛克 IL
120330928 19621126 加里卡特曼 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);
}
// ...
【问题讨论】:
-
使用调试器时变量的值是多少?
-
请记住,
==对字符串进行区分大小写的比较。在比较之前,您可能需要转换为全部小写或全部大写。 -
您的
cout行中充满了>>.. 我什至没有