I am having a problem creating an algorithm to search a text file for a specific keyword. The text file is a list of customer profiles (company name, address, ect...). The program should ask for a search string, which could be the whole company name or just the first few letters of it, and search each entry in the file until it finds it. If there are multiple hits ( ie if there are two companys that start with 'american') then it gives a list of options to choose from then displays the complete company profile. The problem is that once I find the correct name the rest of the profile does not load up. And now, without changing any of the code, the program locks up right after the while loop starts, where it did not before. To save space I'll only list the parts of the program that are not working correctly.
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int viewprof(); // Function to view a customer profile
int findprof(); // Function to find a profile from disk
struct customer // define the structure for customer information
{
char custname[25], custaddress[25], custcity[15], custstate[3];
int custzip, custroute, custcloset;
char custcontact[20], custphone[13];
};
customer custinfo; // define an instance of struct customer
int main()
{
//.......
}
int findprof()
{
int y=0; // controls where to look in disk file for structs
int tempfileloc[5]; // location of structs in disk file
int z; // selects which customer profile to load if multiples
int p; // size of individual customer profile
int a=0; // var for tempfileloc array members
string name; // search variable
char tempname[25], tempaddress[25];
system("cls");
cout<<"enter search name : ";
cin>> name;
int namelength=name.size();
for (int i=0; i<=namelength; i++)
{
// change name to all cap's
name[i]=toupper(name[i]);
}
ifstream fcust("c:\\dispatch_planner\\cust_data.txt", ios::binary | ios::in);
if (fcust.fail()) {cout<<"\a";return 0;}
while (!fcust.eof())
{
p=(y*sizeof(custinfo))-(y*3);
fcust.seekg( p, ios::beg );
if (!fcust.eof())
{
fcust.read(tempname,25);
int x=0; int match=1;
for (x=0; x < namelength; x++)
{
// if a nonmatching character is found,
// unflag 'match' and break out of for loop
if (name[x] != tempname[x]){match=0; x=x+20;}
}
if (match==1)
{
// if 'match' is still flagged, print name and address
// and mark struct location in file location array
fcust.read(tempaddress,25);
tempfileloc[a]=p;
cout<<a+1<<"> "<<tempname<<" "<<tempaddress<<"\n";
a++;
}
y++;
}
else break;
fcust.close();
}
if (a==0) {cout<<"\nNo matching files to display!\n";}
else {cout<<"\nSelect profile number";}
cout<<"\n<0> to exit : ";
cin>>z;
if (z != 0)
{
ifstream fcust("c:\\dispatch_planner\\cust_data.txt", ios::binary | ios::in);
fcust.seekg(tempfileloc[z-1], ios::beg);
fcust.read(custinfo.custname,25);
fcust.read(custinfo.custaddress,25);
fcust.read(custinfo.custcity,15);
fcust.read(custinfo.custstate,3);
fcust.read(reinterpret_cast<char*>(&custinfo.custzip), sizeof(int));
fcust.read(reinterpret_cast<char*>(&custinfo.custroute), sizeof(int));
fcust.read(reinterpret_cast<char*>(&custinfo.custcloset), sizeof(int));
fcust.read(custinfo.custcontact,20);
fcust.read(custinfo.custphone,13);
fcust.close();
}
return z;
}