Program Ignoring the Scanf

Asked

Viewed 2,198 times

1

I need to create an algorithm as requested below, but every time I run the program it "skips" steps. For example:

I want you to stay like this

Product: "Potato"

Sector: "Food"

Quantity: "15"

Price: "15.23"

But it prints on the screen and ignores the scanf

inserir a descrição da imagem aqui

Down with the exercise

Be an algorithm to control the stock products of a supermarket. To each product has the following fields:

  • Name: string size 15.
  • Sector: character
  • Quantity: integer
  • Price: real //price per unit of product

a) Write definition of the product structure

b) Declare the stock vector of the above defined structure type, size 100 and overall.

c) Create a menu for:

C1. Set a block of instructions to read the stock vector.

C2.Define an instruction block that receives a sector and return the number of different products in that sector.

C3. Define a block of instructions that calculates and returns the total capital invested in supermarket products.

C4.Exit from the Program.

//Controle Estoque de produtos de Supermercado
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

struct produto
{
    char nome[15];
    char setor[15];
    int quantidade;
    float preco;
};

struct produto prod[2];

int estoque(struct produto prod[2];)
{
    int i;
    for (i = 0;i < 2; i++)
    {
        printf("\nDigite o nome do produto: ");
        scanf("%s", &prod[i].nome);
        printf("\nDigite o setor: ");
        scanf("%c", &prod[i].setor);
        printf("\nDigite a quantidade: ");
        scanf("%d", &prod[i].quantidade);
        printf("\nDigite o preço: ");
        scanf("%f", &prod[i].preco);
    }
}

int verificar_setor(struct produto prod[2];)
{
    int i, p = 0;
    char ver[15];

    printf("\nDigite o setor: ");
    scanf("%c", &ver);
    for (i = 0;i < 2; i++)
    {
        if (strcpy(ver,prod[i].setor))
        {
            p++;
        }
    }
    printf("Existem %d produtos cadastrados neste setor.", p);
}

int capital(struct produto prod[2];)
{
    int i;
    float c = 0.0;

    for (i = 0;i < 2; i++)
    {
        c = c + prod[i].preco;
    }
    printf("Foi investido um total de %2.f Reais em produtos no Supermercado.", c);
}


int main()
{
    setlocale(LC_ALL,"Portuguese");
    int n = 0;
    printf("Escolha uma opção");
    do
    {
        printf("\n1 - Cadastrar produtos.");
        printf("\n2 - Verificar quantos produtos existem em um determinado setor.");
        printf("\n3 - Total de capital investido nos produtos do Supermecado.");
        printf("\n4 - Sair do Programa.\n");
        scanf("%d", &n);

        switch (n)
        {
            case 1:
                estoque(prod[2]);
                n = 0;
                break;

            case 2:
                verificar_setor(prod[2]);
                n = 0;
                break;

            case 3:
                capital(prod[2]);
                n = 0;
                break;

            case 4:
                printf("Pressione qualquer tecla para sair...");
                system("Pause");
                break;
        }
    }while (n == 0);

    return 0;
}

2 answers

1


Sometimes there is a kind of "garbage" in the keyboard buffer that is when you press enter, then when you enter the next scanf it receives this enter again and for it is as if it had already inserted a value, to solve you can use this line before the scanf fflush(stdin); (In windows, Linux is another command, if I’m not mistaken) or just give a space inside the quotes of the scanf that also works: scanf(" %d", &seuInt);, by default I always give this space inside the scanf to not need to use the fflush, use the fflush just before getchar()

1

Always pay close attention to the compiler warnings, as they are almost always errors. In your case there are several confusions with the types in scanf as well as whether or not &.

Relevant points to be understood:

  • scanf must receive the memory address where it will put the read data
  • & allows you to obtain a memory address associated with a variable

So if you have an integer you need to use & to indicate the place where you will store the whole:

int x;
scanf("%d", &x);

But if you have an array of characters, which we usually call string, will no longer use the & because the variable that represents the array is actually a pointer to the first element, so it is the memory address of the first element:

char palavra[20];
scanf("%s", palavra);
//         ^--- sem &

That said, let’s look at the various places where it is not correct in its code:

printf("\nDigite o nome do produto: ");
scanf("%s", &prod[i].nome);

The field nome of a product is an array of char so it doesn’t take &.

printf("\nDigite o setor: ");
scanf("%c", &prod[i].setor);

setor is the same kind as nome so it can’t be %c and yes %s in the same without the &, for the same reason as above.

char ver[15];
printf("\nDigite o setor: ");
scanf("%c", &ver);

Here the same as above should be %s without &.

You also have some functions of the integer type that do not return value. Confirm which type you really want to use and if it is int you have to put the return appropriate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.