Reputation: 93
Absolute beginner here (FIRST POST) and I am just about finished an assignment where I have had to create a program that lets the user create employee wages slip and then displays them by searching by Surname, Forename, Employee Number, etc.
I am having a problem where If I create a record for Nancy Davidson for e.g. I can output this record properly if I search for EXACTLY Nancy or Davidson. If I search for nancy or davidson it doesn't find it.
I am using a struct to store each employee details, writing them to a data file then reading that file to display the record.
Is there a way that I can get the record to still display even if I search for NAncY?
This my code for my Search By Surname function:
//Record search by employee SURNAME only
void searchSurname(Employee data[], int row)
{
string surname, again;
double wholeTot=0, wholeNet=0;
again = "y";
while (again=="y"||again=="Y")
{
row=0;
bool found = false;
clrscr();
cout << "Please enter Employee SURNAME : ";
Input(surname);
clrscr();
cout << "Surname Search results for " << surname << ". \n\n\n";
readFile (data, row);
int stop=row;
for ( row = 0; row < (stop) ; row++ )
if (surname == data[row].surname)
{
deconvertDate(data[row].date);
cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl
<< " # Employee Number - " << RIGHT(19,2) << data[row].empnum << " #" << endl
<< " # Employee Surname - " << RIGHT(18,2) << data[row].surname << " #" << endl
<< " # Employee Forename - " << RIGHT(17,2) << data[row].forename << " #" << endl
<< " # Department Number - " << RIGHT(17,2) << data[row].dept << " #" << endl
<< " # Normal Hours Worked - " << RIGHT(15,2) << data[row].hours << " #" << endl
<< " # Overtime Hours Worked - " << RIGHT(13,2) << data[row].ohours << " #" << endl
<< " # Pay Rate - " << RIGHT(26,2) << data[row].rate << " #" << endl
<< " # Gross Pay - " << RIGHT(25,2) << data[row].grosspay << " #" << endl
<< " # Tax - " << RIGHT(31,2) << data[row].tax << " #" << endl
<< " # National Insurance - " << RIGHT(16,2) << data[row].natin << " #" << endl
<< " # Total Deductions - " << RIGHT(18,2) << data[row].totalDeduct << " #" << endl
<< " # Net Pay - " << RIGHT(27,2) << data[row].net << " #" << endl
<< " # Week Ending - " << RIGHT(23,2) << data[row].date << " #" << endl
<< " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl << endl << endl;
wholeTot+=data[row].grosspay;
wholeNet+=data[row].net;
cout << "The total recorded GROSS PAY of " << data[row].surname << " is :" << wholeTot << endl;
cout << " and the total recorded NET PAY is :" << wholeNet << endl << endl;
found = true;
}
else
if (found = false)
{
cout << "No results found for that SURNAME!" << endl;
}
Upvotes: 1
Views: 6673
Reputation: 153977
When you call std::equal
for the comparison, you can give it
a fourth argument with a comparitor. Just write a comparator
which does case insensitive comparison:
struct CaseInsensitiveCmp
{
bool operator()( char lhs, char rhs ) const
{
return ::tolower( static_cast<unsigned char>( lhs ) )
== ::tolower( static_cast<unsigned char>( rhs ) );
}
};
(This uses the one argument tolower
function in <ctype.h>
,
which is the simplest solution for a beginner. In production
code, of course, you'd use the std::ctype
facet in
<locale>
.)
Upvotes: 3
Reputation: 19873
The trick is usually to convert both the stored record and the user input record in either lower case or upper case. For this you can use toupper or tolower
Upvotes: 3
Reputation: 249394
You want to do case-insensitive comparison. strcasecmp
might be a good start: http://linux.die.net/man/3/strcasecmp
Upvotes: 2