简体   繁体   English

在结构数组中搜索用户输入的字符串

[英]Searching through struct array for string entered by user

This is just a function from my program that is supposed to search for the string or integer the user enters in the struct array. 这只是我程序中的一个函数,应该搜索用户在struct数组中输入的字符串或整数。 What I'm trying to do is add an error message and the ability to try again when that entered string or integer is not found in the struct array. 我正在尝试做的是添加一条错误消息,并且当在结构数组中找不到所输入的字符串或整数时可以再次尝试的功能。 It searches just fine if you enter the correct string or integer but nothing happens if you don't. 如果您输入正确的字符串或整数,它的搜索就很好,但是如果没有输入,则什么也不会发生。 I'm trying to change that but can't find the solution. 我正在尝试更改它,但找不到解决方案。

I've tried for a while now with one of the cases in my switch statement but I need to do it for all three. 我已经在switch语句中尝试了一种情况,但已经尝试了一段时间,但我需要对这三种情况都进行此操作。 But so far I've only tried on case 3. 但是到目前为止,我仅尝试了案例3。

search(struct items aItems[], int *num_items)
{
    int choice_of_search, b=0, found_word=0, search_num, i=0, j=0, k=0;
    char search_phrase[20]; struct items search[MAX];

    printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
    scanf("%d", &choice_of_search);

    while(choice_of_search < 1 || choice_of_search > 3)
    {
        printf("Wrong choice!\n");
        printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
        scanf("%d", &choice_of_search);
    }


    switch(choice_of_search)
    {
        case 1:
            printf("Item number?\n");
            scanf("%d", &search_num);
            for(i = 0; i < *num_items; i++)
            {
                if(search_num == aItems[i].itemnumber)
                {
                    printf("Item number found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                }
            }
            break;

        case 2:
            printf("Name?\n");
            scanf("%s", search_phrase);
            for(i = 0; i < *num_items; i++)
            {
                if(strstr(aItems[i].name, search_phrase))
                {
                    printf("Name found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                }
            }
            break;

        case 3:
            printf("Balance?\n");
            scanf("%d", &search_num);
            for(i = 0; i < *num_items; i++)
            {
                if(search_num == aItems[i].balance)
                {
                    printf("Balance found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                }
                else
                {
                    printf("Balance not found! Try again.\n");
                    printf("Balance?\n");
                    scanf("%d", &search_num);
                }
            }
            break;
    }


    while(b < found_word)
    {
        printf("Item number: %d Name: %s  Balance: %d\n", search[b].itemnumber, search[b].name, search[b].balance);
        b++;
    }
}

Maybe this can help 也许这可以帮助

int done;
...
...

    case 3:
        done = 0;
        while(1);
        {
            printf("Balance?\n");
            scanf("%d", &search_num);
            for(i = 0; i < *num_items; i++)
            {
                if(search_num == aItems[i].balance)
                {
                    printf("Balance found!\n");
                    search[found_word]=aItems[i];
                    found_word+=1;
                    done = 1;
                }
            }
            if (done) break;

            printf("Balance not found! Try again.\n");
        } 
        break;

But notice that the code isn't user friendly as it doesn't allow the user a way to stop the search without a match. 但是请注意,该代码不是用户友好的代码,因为它不允许用户在没有匹配项的情况下停止搜索。 So maybe you should consider adding a "Would you like try again" option. 因此,也许您应该考虑添加“您想重试”选项。

A simple approach could be 一个简单的方法可能是

            printf("Balance not found! Would like to try again?.\n");
            scanf(" %c", &some_char);
            if (some_char != 'y') break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM