0

how are you? I'm having difficulty designing a program and would like to know if you can help me with it. The work is about making a product registration system for a company. To explain better, some things I was able to do:

  1. I created the struct of the products with name, value and we chose flavor and filling as the characteristics
  2. I created a void main menu with 4 possibilities (the 3 described in the work + 1 to terminate the program)
  3. I created a void with return for the user's choice of possibilities
  4. I created the void registration based on the struct product, but I'm not able to find a way to repeat it without setting a value for the for; however, I didn't want to have set a repeat value.

After that, this would be my question:

  1. How to print the table of products that the customer registered with two characteristics together (flavor and filling).

Thank you in advance for any help, opinion and criticism.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct produto
{
    char nome[30];
    float valor;
    int sabor;
    int recheio;

};


void menu_principal()
{
    printf("\t\t\tMENU PRINCIPAL");
    printf("\n1: Digite 1 para incluir um produto");
    printf("\n2: Digite 2 para imprimir a lista de produtos");
    printf("\n3: Digite 3 para consultar o valor de um produto");
    printf("\n4: Digite 4 para encerrar o programa");
}

void menu_escolha(char *escolha)
{
    printf("\nDigite sua escolha: ");
    scanf(" %c", &(*escolha));
}

void cadastro(struct produto p[])
{
    int i, quant = 0;
    printf("\t\t\tCADASTRO DE PRODUTO");
    printf("\nQuantos produtos voce ira cadastrar? ");
    scanf("%d", &quant);
    for(i = 0; i < quant; i++)
    {

        fflush(stdin);
        printf("\nNome do produto %d: ", i + 1);
        gets(p[i].nome);

        do
        {
            fflush(stdin);
            printf("Sabor do produto %d (1: doce / 2: salgado): ", i + 1);
            scanf("%d", &p[i].sabor);
        }
        while(p[i].sabor < 1 || p[i].sabor > 2);


        do
        {
            fflush(stdin);
            printf("Recheio do produto %d (1: chocolate / 2: baunilha / 3: frango / 4: carne): ", i + 1);
            scanf("%d", &p[i].recheio);
        }
        while(p[i].recheio < 1 || p[i].recheio > 4);

        do
        {
            fflush(stdin);
            printf("Valor do produto %d (ex.: 1.23): ", i + 1);
            scanf("%f", &p[i].valor);
        }
        while(p[i].valor < 0);

    }

}

void impressao_sabor(struct produto p[], int tipo_um)
{
    int i;

    for(i = 0; i < 30; i++)
    {
        if(p[i].sabor == tipo_um)
        {
            printf("\nNome do produto : %s",  p[i].nome);
            printf("\nSabor do produto : %d",  p[i].sabor);
            printf("\nRecheio do produto : %d",  p[i].recheio);
        }
    }
    printf("\n");
}

void impressao_recheio(struct produto p[], int tipo_dois)
{
    int i;
    for(i = 0; i < 3; i++)
    {
        if(p[i].recheio == tipo_dois)
        {
            printf("Nome do produto %d: %s", i+1, p[i].nome);
            printf("Sabor do produto %d: %d", i+1, p[i].sabor);
            printf("Recheio do produto %d: %d", i+1, p[i].recheio);
        }
    }
    printf("\n");
}

void main()
{
    struct produto p[30];
    char escolha;
    char decisao;

    menu_principal();
    menu_escolha(&escolha);

    switch(escolha)
    {
    case '1':
        cadastro(p);
        printf("\nAdicionar mais um produto? s/n: ");
        scanf(" %c", &decisao);
        printf("\n");

        do
        {
            if(decisao == 's')
            {
                cadastro(p);
                printf("\nAdicionar mais um produto? s/n: ");
                scanf(" %c", &decisao);
                printf("\n");
            }
        }
        while(decisao == 's');

        if(decisao == 'n')
        {
            menu_principal();
            menu_escolha(&escolha);
        }

    case '2':
        printf("\t\t\tLISTA DOS PRODUTOS");
        printf("\nSabores dos produtos");
        printf("\nDoce");
        impressao_sabor(p,1);
        printf("\nSalgado");
        impressao_sabor(p,2);
        ;
    case '3':
        //consulta(p);
        //menu_principal();
        break;
    case '4':
        printf("\nObrigada por usar o programa!");
        break;
    default:
        printf("Opcao invalida!");
        menu_escolha(&escolha);
    }
}

1 Answers1

1

You just pass the field names which you want to print.

You can use enum to declare the field types. For example:

enum field_type { nome = 1, valor = 2, sabor = 4, recheio = 8 };

Note in this example the numbers chosen are powers of 2: 1, 2, 4, 8 You can use bit-wise OR operator to combine these fields, and use bit-wise AND operator to extract the fields.

#include <stdio.h>

struct produto
{ char nome[30]; float valor; int sabor; int recheio; };

enum field_type { nome = 1, valor = 2, sabor = 4, recheio = 8 };

void print(struct produto p[], int count, int fields)
{
    for (int i = 0; i < count; i++) 
    {
        if (fields & nome)    printf("%s ", p[i].nome);
        if (fields & valor)   printf("%.2f ", p[i].valor);
        if (fields & sabor)   printf("%d ", p[i].sabor);
        if (fields & recheio) printf("%d ", p[i].recheio);
        printf("\n");
    }
}

int main(void)
{
    struct produto p[] = {
        { "nome1", 0.1f, 100, 1000 },
        { "nome2", 0.2f, 200, 2000 },
        { "nome3", 0.3f, 300, 3000 }};
    int count = 3;

    printf("print by nome:\n");
    print(p, count, nome);

    printf("print by nome + valor + recheio:\n");
    print(p, count, nome | valor | recheio);
}

Aside, fflush(stdout) is okay, but there is no fflush(stdin) in the C standard. See comments and links.

If you want to read an integer and ignore the rest of the line, consider using a different method, for example

char buf[100];
int i;

printf("Enter integer:\n");
fgets(buf, sizeof(buf), stdin);
i = atoi(buf);
printf("You entered %d\n", i);

Otherwise just use

if (sscanf("%d", &i) == 1) 
    printf("You entered %d\n", i);

Use int main(void) instead of void main()

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77