接受用户输入,无论他们在c++中输入大写还是小写
User input accepted whether they input uppercase OR lowercase in C++
这里的绝对初学者(第一篇文章),我刚刚完成了一项任务,我必须创建一个程序,让用户创建员工工资条,然后通过姓氏,名称,员工编号等搜索显示它们。
我有一个问题,如果我为南希戴维森创建一个记录,例如,如果我搜索南希或戴维森,我可以正确地输出这个记录。如果我搜索nancy或davidson,它找不到它。
我使用一个结构体来存储每个员工的详细信息,将它们写入数据文件,然后读取该文件以显示记录。
是否有一种方法,我可以让记录仍然显示,即使我搜索NAncY?
这是我的搜索姓氏函数的代码:
//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 << ". nnn";
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;
}
调用std::equal
进行比较时,可以给出它与比较者的第四个争论。只要写一个比较器执行不区分大小写的比较:
struct CaseInsensitiveCmp
{
bool operator()( char lhs, char rhs ) const
{
return ::tolower( static_cast<unsigned char>( lhs ) )
== ::tolower( static_cast<unsigned char>( rhs ) );
}
};
(这使用<ctype.h>
中的一个参数tolower
函数,这对初学者来说是最简单的解决方案。在生产中代码,当然,您将使用
中的std::ctype
方面。<locale>
)。
技巧通常是将存储的记录和用户输入记录转换为小写或大写。你可以使用
您需要进行不区分大小写的比较。strcasecmp
可能是一个好的开始:http://linux.die.net/man/3/strcasecmp
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type