want to create a struct of variable size and error program

Asked

Viewed 43 times

-3

i am trying to create a struct of variable size but always q do it in the second run of the loop the program hangs

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

struct produto{
    int codProd,quantProd;
    float precoProd;
    char nomeProd[6];
};
main(){
    int opcao,codigo,quantVend,y;
    produto roupas[y];
    y = 3;
    do{
        printf("1 cadastro\n");
        printf("2 vendas\n");
        printf("3 relatorio\n");
        printf("4 preco\n");
        printf("0 sair\n");
        scanf("%d", &opcao);
        if(opcao == 1){
            printf("digite a quantidade de produtos que vai registrar\n");
            scanf("%d",&y);
            for(int x=0;x<y;x++){
                fflush(stdin);
                printf("codigo");
                scanf("%d",&roupas[x].codProd);
                fflush(stdin);
                printf("nome do produto:");
                gets(roupas[x].nomeProd);
                printf("quantidade:");
                scanf("%d",&roupas[x].quantProd);
                printf("preco :");
                scanf("%f",&roupas[x].precoProd);

            }
        }
        if(opcao == 3){
            printf("relatorio do estoque\n");
            for(int x=0;x<y;x++){
                printf("codigo: %d\n",roupas[x].codProd);
                printf("nome do produto: %s\n",roupas[x].nomeProd);
                printf("quantidade: %d\n",roupas[x].quantProd);

            }
        }
        if(opcao == 2){
            printf("vendas\n");
            printf("digite o codigo do produto \n");
            scanf("%d",&codigo);
            for(int x=0;x<y;x++){
                if(codigo == roupas[x].codProd){
                    printf("voce vendeu :%s\n",roupas[x].nomeProd);
                    roupas[x].quantProd = roupas[x].quantProd - 1;
                }
            }
        }
    }while(opcao=!0);
}

1 answer

1

The problem is that you are using the variable before initializing

int opcao,codigo,quantVend,y; //Cria a variável y
produto roupas[y]; //Usa a variável y
y = 3; //Atribui um valor a variável y

The order must be this

int opcao,codigo,quantVend,y; //Cria a variável y
y = 3; //Atribui um valor a variável y
produto roupas[y]; //Usa a variável y
  • I understand, is there any way I could assign the value of this variable after I used it ? I need this for a product registration system

  • No, but why do you need to use before initializing?

  • Perhaps dynamic memory allocation helps in solving your problem. Search by malloc function.

  • If you were really using C++ just use the new.

  • William I need to use before because I want the user to decide the size of the struct only for the sistrma to work I need to declare the struct before giving the option to the user to choose the size of it

  • Go through your code, you want one thing, but it feels like you’re doing something else

Show 1 more comment

Browser other questions tagged

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