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

Dani AI

Generated

The immediate reason nothing prints is the scope/ownership of your data: correctly points out that the array filled by read_file() is not the same array passed to search() from main(). The function in the first post creates and fills a local array, then returns (leaving main's small array empty). Beyond that there are a few additional issues to check: use int main() (not void main()), verify the input file actually opens, and fix any stream-operator/semicolon typos that will stop the program compiling.

A safer, modern pattern is to read into a dynamic container and either return it or pass it by reference. Example (different approach than earlier replies):

#include <vector>
#include <fstream>

std::vector<employees> readDatabase(const std::string& path) {
    std::vector<employees> list;
    std::ifstream in(path);
    if (!in) return list; // empty on failure
    employees tmp;
    while (in >> tmp.ss_number >> tmp.dob >> tmp.f_name >> tmp.l_name >> tmp.state)
        list.push_back(tmp);
    return list;
}

void findByName(const std::vector<employees>& list, const std::string& first, const std::string& last) {
    for (const auto& e : list) {
        if (e.f_name == first && e.l_name == last) { print_person(e); return; }
    }
    std::cout << "No match found\n";
}

Quick troubleshooting checklist:

  • Print a count (e.g., list.size()) after reading to confirm data loaded.
  • Confirm the file path and that ifstream opened successfully.
  • Fix compile errors (missing ;, wrong <</>> usage).
  • Consider using std::string for SSNs (preserve leading zeros) and parse DOB into year/month/day if you need date arithmetic.
  • For more robust searches add case-insensitive comparison or allow partial/name-only searches.

Both passing an array by reference (as suggested) and returning a std::vector are valid; returning a vector is more flexible and avoids hard-coded size limits.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.