I can not call a string

1

I've created an algorithm that simulates a stock of a DVD sale. It stores name, price, and quantity. It has some functions and enters them, one that informs the quantity and the price of a specific dvd and it is in that I am having problems.

After calling the function and pulling the struct data, it should display the name, price, and units. My problem is that I can only search and return strings with up to 3 letters.

Example: If I register and then search for "Max", it works fine, but if it is "Maax", it does not find anything.

Follow the code if someone can help me

void pesquisa(struct locadora v4[MAX])
{   int x;
char pesquisa[100];
printf ("\nInforme o nome de qual Dvd você quer buscar: ");
scanf("%s", &pesquisa);
for(x=0;x<MAX;x++)
    if ( strcmp ( pesquisa, v4[x].dvd)==0)
        printf ("\nO preço do dvd %s é R$ %.2f. Ele possui %d unidades em estoque!!\n", v4[x].dvd, v4[x].preco, v4[x].quant);
    else
            printf ("\nNão foi encontrado nenhum registro com esse nome");
}
    
asked by anonymous 14.05.2015 / 20:37

1 answer

3

Your error is in scanf() .

scanf("%s", &pesquisa);

The correct way is

scanf("%s", pesquisa);
//          ^ sem &

You can still limit the possibility of buffer overflow if you enter the maximum number of characters to read

scanf("%99s", pesquisa);

And you should still always check the return value

if (scanf("%99s", pesquisa) != 1) /* erro */;
    
14.05.2015 / 20:44