c++ - 接受用户输入无论他们在 C++ 中输入大写还是小写

标签 c++ uppercase lowercase

这里绝对是初学者(第一篇文章),我刚刚完成一项任务,我必须创建一个程序,让用户创建员工工资单,然后通过按姓氏、名字、员工编号等进行搜索来显示它们。

我遇到了一个问题,如果我为 Nancy Davidson 创建记录,例如如果我完全搜索 Nancy 或 Davidson,我可以正确输出此记录。如果我搜索 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 << ". \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;
                }

最佳答案

当您调用 std::equal 时为了比较,你可以给它 与比较器的第四个参数。只写一个比较器 不区分大小写的比较:

struct CaseInsensitiveCmp
{
    bool operator()( char lhs, char rhs ) const
    {
        return ::tolower( static_cast<unsigned char>( lhs ) )
            == ::tolower( static_cast<unsigned char>( rhs ) );
    }
};

(这在 tolower 中使用一个参数 <ctype.h> 函数, 这是初学者最简单的解决方案。生产中 代码,当然,你会使用 std::ctype刻面
<locale> .)

关于c++ - 接受用户输入无论他们在 C++ 中输入大写还是小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442597/

相关文章:

java - 如何在Java中自动将输入字符转换为大写

vim - 在vim中将指定行更改为大写

c++ - 如何将 IHTMLDocument2 ->get_body ->get_innerHTML 变成小写字符串?

C++20 bit_cast 与 reinterpret_cast

c++ - 通过标准平均 C++ 模糊图像

c++ - 在 C++ 中定义静态成员

PHP大写转小写重写优化?

c++ - 类中的类 : 'is not a type' error

MySQL LOWER() 函数对于 º 字符不是多字节安全的?

python - 在 python 中,如何不更改列表元素中每个字母的大小写?