3
Good morning! I got recovery again in the matter of algorithms, what got me most was structure and passing parameters. I decided to create an activity to train for recovery Monday.
Following the teacher’s slides I finally did, the code is working, which doesn’t explain is... How to run with for, I have a data structure and pass it to a function using pointers.
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
typedef struct mercearia
{
char mercadoria[20];
int quantidade;
int valor;
} mercearia;
void EstruturaCadastro(struct mercearia *x)
{
printf("\n");
printf("Insira nome do produto: ");
scanf("%s", &(*x).mercadoria);setbuf(NULL, stdin);
printf("Insira a quantidade do produto: ");
scanf("%i", &(*x).quantidade);setbuf(NULL, stdin);
printf("Insira o valor do produto: R$ ");
scanf("%i", &(*x).valor);setbuf(NULL, stdin);
}
void ExibirCadastroProdutos(struct mercearia x)
{
printf("\n");
printf("\nO produto cadastrado %s", x.mercadoria);
printf("\nValor quantidade do produto %i", x.quantidade);
printf("\nValor da unidade do produto %i", x.valor);
}
int main(void)
{
setlocale(LC_ALL, "");
struct mercearia produto;
EstruturaCadastro(&produto);
ExibirCadastroProdutos(produto);
}
I figured it would work out this way:
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
typedef struct mercearia
{
char mercadoria[20];
int quantidade;
int valor;
} mercearia;
void EstruturaCadastro(struct mercearia *x)
{
int i;
printf("\n");
for(i = 0; i < 5; i++)
{
printf("Insira nome do produto: ");
scanf("%s", &(*x)[i].mercadoria);setbuf(NULL, stdin);
printf("Insira a quantidade do produto: ");
scanf("%i", &(*x)[i].quantidade);setbuf(NULL, stdin);
printf("Insira o valor do produto: R$ ");
scanf("%i", &(*x)[i].valor);setbuf(NULL, stdin);
}
}
void ExibirCadastroProdutos(struct mercearia x)
{
int i;
printf("\n");
for(i = 0; i < 5; i++)
{
printf("\nO produto cadastrado %s", x.mercadoria);
printf("\nValor quantidade do produto %i", x.quantidade);
printf("\nValor da unidade do produto %i", x.valor);
}
}
int main(void)
{
setlocale(LC_ALL, "");
struct mercearia produto[5];
EstruturaCadastro(&produto);
ExibirCadastroProdutos(produto);
}
But then it returns to me 300 thousand errors... Someone could point out where I’m going wrong?
Errors submitted by the IDE:
||=== Build file: "no target" in "no project" (Compiler: Unknown) ===| C: Users lelre Documents 1 - Personal Faculty ALG List of activities Test archive. c||In Function 'Structurecadastre':|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|22|error: subscripted value is neither array nor Pointer nor vector|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|22|Warning: Passing argument 2 of 'setbuf' from incompatible Pointer type|
C: Program Files (x86) Codeblocks Mingw include stdio. h|193|note: expected 'char *' but argument is of type 'struct FILE *'|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|25|error: subscripted value is neither array nor Pointer nor vector|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|25|Warning: Passing argument 2 of 'setbuf' from incompatible Pointer type|
C: Program Files (x86) Codeblocks Mingw include stdio. h|193|note: expected 'char *' but argument is of type 'struct FILE *'|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|28|error: subscripted value is neither array nor Pointer nor vector|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|28|Warning: Passing argument 2 of 'setbuf' from incompatible Pointer type|
C: Program Files (x86) Codeblocks Mingw include stdio. h|193|note: expected 'char *' but argument is of type 'struct FILE *'|
C: Users lelre Documents 1 - Personal Faculty ALG List of activities Test archive. c||In Function 'main':|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|50|Warning: Passing argument 1 of 'Structurecadastre' from incompatible Pointer type|
C: Users lelre Documents 1 - Personal Faculty ALG List of activities Test archive. c|14|note: expected 'struct mercearia ' but argument is of type 'struct mercearia ()[5]'|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|51|error: incompatible type for argument 1 of 'Displaysroducts'|
C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|32|note: expected 'struct mercearia' but argument is of type 'struct mercearia *'| ||=== Build failed: 4 error(s), 4 Warning(s) (0 minute(s), 0 Second(s)) ===|
Good morning Lelre! If possible, please put the errors presented along with your question, to facilitate the analysis of the problem.
– Sérgio Lopes
Good morning boss! I added the bugs presented by the IDE.
– user70896