用户输入是否在C ++中输入大写或小写

时间:2013-05-08 13:55:30

标签: c++ uppercase lowercase

这里的绝对初学者(FIRST POST)我刚刚完成了一项任务,我必须创建一个程序,让用户创建员工工资单,然后通过按姓氏,姓名,员工编号等搜索来显示它们。

我遇到了一个问题,如果我为南希戴维森创建一个记录,例如如果我搜索完全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;
                }

3 个答案:

答案 0 :(得分:3)

技巧通常是以小写或大写形式转换存储的记录和用户输入记录。为此,您可以使用touppertolower

答案 1 :(得分:3)

当你致电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>。)

答案 2 :(得分:2)

您想要进行不区分大小写的比较。 strcasecmp可能是一个好的开始:http://linux.die.net/man/3/strcasecmp