I want to read a line (record) of a CSV file. And so as to increase accuracy of hitting the correct record and then adding some information if a record is found, i am searching the record using four strings.
However, this loop is reading many lines from the file, looping through them (and flashing them to the screen one after the other- so irritating) , then displays all of them.
What could be wrong ? i want to hit and display the only record that coincides with the entered input.
void search_record ();
int main ()
{
search_record ();
}
void search_record (){
std::string line = "";
std::string OwnerIdCard, Surname, LandNum, KraPin ;
std::fstream inFile("Lands File.txt"); //open text file in read mode
std::cout << "Please enter the individual's Surname whose Land you want to search "<<endl;
std::getline (cin, Surname);
std::cout<<"Enter Land number\n";
getline (cin, LandNum);
std::cout<<"Enter owner KRA pin\n";
getline (cin, KraPin);
std::cout<<"Enter Owner id card number\n";
getline (cin, OwnerIdCard);
std::string::size_type offset;
while (std::getline(inFile, line)) //parsing lines
{
//to increase accuracy, i am searching the file using the four strings at once
if ((offset = line.find (Surname, 0)) && (offset = line.find (LandNum,0) && (offset = line.find (KraPin,0) && (offset = line.find (OwnerIdCard,0) !=string::npos))));
std::cout <<"\n"<<Surname<<" ID Number "<< OwnerIdCard <<" Owner of Land number "<<LandNum<<" with KRA Number "<<KraPin<<"record found with the following details: \n\n"<<line<<"\n"<<endl;
}
{
system ("pause");
inFile.close();
return ;
}
File being read has many records like this:
1 2 3
Surname:nyinyi, OtherNames:wao, Land Number:098765, KRA PIN:xbxbbmbmb, ID card No.:25546675, address:125-Cape Town, County:Transavaal, District: Cape, Division:sjgjkgkgkl, Location:sjjgkjgk, Sublocation:sggjgkgkgk
Look closely at that if() statement on line 38, it's probably the cause of your problems.
You probably should be comparing each find() statement with std::string::npos, not just that last comparison. Also why are you using the assignments in that if() statement, you never actually use offset so you really don't need it:
Just wondering is the search on all these terms redundant? Guessing (based on my own experience with Cadastral DB's) the LandNum sounds as though it should be unique, and KraPin is a owner's PIN number .
Even if you were searching based on non-unique terms such as owners names, could you do it in a cascading fashion: The surname search returns a vector of all records with that surname, then following searches use that reduced set to work with.
With the Westminster Title system, one doesn't have to go too far before some unique value is found. The Title Number is unique, some jurisdictions have this as being the Lot Number/ Plan number. Others have the more traditional Volume/Folio/CT system. Some systems are more complicated because there is not a one to one relationship between a parcel and the appellation (The descriptor: eg Lot 1 Plan 1030478 ). An example of this is where several distinct parcels have the same descriptor, and typically belong on the same title.
So a search on the surname and the Plan number should work for most situations, you could make sure these searches return a reference to a std::vector just in case they don't result in 1 record. Even easier if LandNum is unique.