在CHAR数组中搜索客户名称 [英] Searching a CHAR Array for Customer Name

查看:82
本文介绍了在CHAR数组中搜索客户名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的学校做这件事,而不是寻找要查找客户的帐号,而是寻找客户名..通常的strcmp(customerNames [index],searchCustomer )== 0)对此无效.似乎这仅是一个值,而我拥有的这个customerNames数组具有4个不同的预设名称,以及您在运行时添加到其中的任何名称.任何帮助表示赞赏;)

I''m trying to do this for my school, instead of searching for the account number to find the customer, I''m having to find the customer name instead..The usual strcmp ( customerNames[index], searchCustomer ) == 0 ) doesn''t work on this. It seems that is only for one value, while this customerNames array that I have has 4 different preset names, plus whatever you add to it in run-time. Any help is appreciated ;)

//--Main Function
int main()
{
    int mainMenuSelection;
    int accountArray[SIZE] = {1202, 5841, 5644, 5481};
    double balanceArray[SIZE] = {450.23, 541.25, 1027.27, 108.50};
    char customerNames[SIZE][25] = {"George Washington", "John Smith", "Jane Smith", "Adam Smith"};

    int index;

    mainMenu(accountArray, balanceArray, customerNames);


    system("pause");
    return 0;
}
//--------------------------------------------------------------------------------



void searchByName ( int accountArray[], double balanceArray[], char customerNames[][25])
{



char searchCustomer;
    int index = 0, found = -1;
    double amount = 0;
    printf ("Enter the account to search for:");
    fflush ( stdin );
    scanf ("%[^\t\n]s", &searchCustomer);


    while ( index < SIZE )
    {
        if ( customerNames[index] == searchCustomer  )
        {
            found = index;
            break;
        }
        index++;
    }

    if ( found == -1 )
    {
        printf ("That account doesn''t exist!\n\n");
        system("pause");
        bankerSide(accountArray, balanceArray, customerNames );
    }
    else
    {
        printf ("Here is the customer :\n");
        printf("\t%s\tAcc#%i\n", customerNames[index], accountArray[index]);
        system("pause");
        bankerSide(accountArray, balanceArray, customerNames );

    }
}
//--------------------------------------------------------------------------------

推荐答案

尝试使用strcmp() ...比较字符串的方法有很多种,但这可能是最简单的方法之一在概念上.

不过,将来还有其他事情要考虑(您应该作为学生考虑):
-空白(在名字或姓氏之前或之后)会发生什么?
-如果瓶盖不完全在同一位置会怎样?

所有字符串容器类都具有某种比较器,请对其进行查找,以便您可以比较和对比不同的方法.
Try using strcmp()... there''s a variety of different methods for comparing strings, but that''s probably one of the simplest in concept.

There''s other things to consider in the future though (that you should consider as a student):
- What happens with white space (before or after the first or last name)?
- What happens if caps aren''t exactly in the same spot?

All string container classes have some sort of comparator, look them up so you can compare and contrast the different methods.


这篇关于在CHAR数组中搜索客户名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆