Program Ignoring Scanf

1

I need to create an algorithm as a request below, however, 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 scanf

Downtheexercise

Beanalgorithmtocontrolproductsfromasupermarketstock.Foreachproduct,wehavethefollowingfields:

  • Name:stringofsize15.
  • Sector:character
  • Amount:integer
  • Price:real//priceperunitofproduct

a)Writethedefinitionoftheproductstructure

b)Declarethestockvectorofthetypeofstructuredefinedabove,size100andglobal.

c)Createamenufor:

c1.Defineaninstructionblocktoreadthestockvector.

c2.Defineaninstructionblockthatreceivesasectorandreturnsthenumberofdifferentproductsinthatsector.

c3.Defineablockofinstructionsthatcalculatesandreturnsthetotalcapitalinvestedingroceries.

c4.ExitProgram.

//ControleEstoquedeprodutosdeSupermercado#include<stdio.h>#include<stdlib.h>#include<string.h>#include<locale.h>structproduto{charnome[15];charsetor[15];intquantidade;floatpreco;};structprodutoprod[2];intestoque(structprodutoprod[2];){inti;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;
}
    
asked by anonymous 13.05.2018 / 22:12

2 answers

1

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

    
13.05.2018 / 22:30
1

Always take extreme care of compiler warnings, as they are almost always errors. In your case there are several confusions with types in scanf as well as whether or not & .

Relevant points to realize:

  • 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 where to save the integer:

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

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

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

So, let's look at the various places where it is not correct in your code:

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

The nome field of a product is an array of char logo does not carry & .

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

setor is of the same type as nome logo can not be %c and yes %s in same without & , 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 & .

There are still some integer functions that do not return a value. Confirm which type you really want to use and if you are int you must put return appropriate.

    
13.05.2018 / 23:07