{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);
}

You declare a small array in main.
The read_file declares and fills a large array.
Back in main you pass that small, empty array to the search function. There's nothing for it to search.

How about:

int main()
{
    employees array[1000];
    read_file(array);
    search(array);
    return 0;
}


void read_file( employee array[] )
{
    //rest of your function, minus the array declration

A little indenting in your code would also help keep it clear what each set of { } encloses.

Edited 2 Years Ago by vmanes

This article has been dead for over six months. Start a new discussion instead.