Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c - Searching for a string in a struct

The code below is a code that will track my product costs and remaining quantity.The problem I'm facing with is I can't search the code by

if(g[n].name == search[10])

The out put keep showing

"Item not found"

Im a beginner of c language and was hope to learn more. Please correct my code and send it here so that I can know why my code is wrong.

#include <stdio.h>

struct product
{
    char name[10];
    int quantity;
    float costs;
};

void fn_search (struct product g[]);

int main ()
{
    int n;

    struct product g[4];

    strcpy(g[0].name,"aa1");
    g[0].quantity = 10;
    g[0].costs = 1;

    strcpy(g[1].name,"bb2");
    g[1].quantity = 10;
    g[1].costs = 2;

    strcpy(g[2].name,"bb3");
    g[2].quantity = 10;
    g[2].costs = 3;

    fn_search (g);
}

void fn_search (struct product g[10])
{
    int n;
    char search[10];
    printf("Search>> ");
    scanf("%s",&search[10]);

    for (n=0;n<4;n++)
        {
            if(g[n].name == search[10])
                {
                   printf ("
costs = NTD%.2f",g[n].costs);
                    printf ("
quantity = %d
",g[n].quantity);
                }
            else
                {
                    printf("
Item not found.");
                    break;
                }
        }
}
question from:https://stackoverflow.com/questions/65625776/searching-for-a-string-in-a-struct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two bugs:

Incorrect use of scanf :

scanf("%s",&search[10]);  --> scanf("%9s", search);

Note: scanf("%9s", &search[0]); is also fine but the above is the common way.

Incorrect string compare :

if(g[n].name == search[10]) --> if(strcmp(g[n].name, search) == 0)

Also notice that you never initialized g[3] but fn_search checks it.

Then this part:

        else
            {
                printf("
Item not found.");
                break;
            }

means that you break the for loop as soon as an item doesn't match. In other words: Currently you only compare against g[0]

You don't want that! Check all items before printing "Item not found".

So the for loop should be more like:

for (n=0;n<4;n++)
{
    if(strcmp(g[n].name, search) == 0)
    {
        printf ("
costs = NTD%.2f",g[n].costs);
        printf ("
quantity = %d
",g[n].quantity);

        return;  // Exit function when match is found
    }
}

// When execution arrives here, there was no matching element
printf("
Item not found.");

Finally:

void fn_search (struct product g[10])
                                 ^^
                                 why ??

Either do

void fn_search (struct product g[])

or

void fn_search (struct product *g)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...