-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);
}
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
– cesar xexeu
No, but why do you need to use before initializing?
– Costamilam
Perhaps dynamic memory allocation helps in solving your problem. Search by malloc function.
– anonimo
If you were really using C++ just use the new.
– anonimo
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
– cesar xexeu
Go through your code, you want one thing, but it feels like you’re doing something else
– Costamilam