gin,
Your program has minor problems with declarations, things your compiler might have shown, like
preco float;
instead of float preco;
in the statement
scanf()
to read color, a char[100]
, using specifier %f
scanf()
to read preco
, one float
, using %d
printf()
to show cor
, char[100]
, using %i%%
- field use
desconto
that does not appear in CARRO
Perhaps you could have solved this more easily just by tracking the compiler’s messages. But that’s not all:
about scanf()
scanf()
returns a value, and you should really test it. The value is the total of read items and will tell you if scanf()
managed to read something. If you do not treat this return will take a risk letting the program follow blindly
gets()
Do not use gets()
. Use fgets()
because it limits the size of what it can read. If the user type beyond the field capacity will cancel your program
free()
you allocate memory for a single CAR, but release it nowhere. Even in such simple cases you must confirm that you released every byte you allocated. Your next programs should be more sophisticated and will benefit from this habit. Or you will be penalized for the lack of it.
New line when reading field
Don’t do that. Leave the reading right after the question prompt . It’s like that everywhere for a reason: it’s better to keep the context
iteration
Almost certainly did not intend to read a single CARRO
then your structure won’t work. You need a vector of cars, or a connected list of cars, something to store the information in memory. And you didn’t do that.
prototypes
main()
should be the first function of your program, always. In general in a separate file. The reason is simple, since the execution always starts in main()
. Use prototypes, along with structure statements, if possible in a header file --- . h --- separate. There’s a reason it’s been done since the '70s.
See the example below
a version of your code that at least runs
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
typedef struct {
char marca[100]; //vetor de char, representa um string
int ano_de_Fabricacao;
char cor[100];
float preco; //considerar descontos inteiro em % (20%, 5%)
}CARRO;
CARRO* criarCarro() {
CARRO* ptr_carro = (CARRO*)malloc(sizeof(CARRO));
if (ptr_carro == NULL) {
printf("Memória insuficiente! Não alocar espaço para produto.");
exit(1);
}
return (ptr_carro);
}
void lerDados(CARRO* novoCarro) {
printf("Digite a MARCA do produto: \n");
gets(novoCarro->marca);
printf("Digite o ANO_DE_FABRICACAO do produto: \n");
scanf("%d", &novoCarro->ano_de_Fabricacao);
printf("Digite o COR do produto: \n");
scanf("%f", &novoCarro->cor);
printf("Digite o PRECO do produto: \n");
scanf("%d", &novoCarro->preco);
}
void imprimirDados(CARRO* novoCarro, float valorVenda) {
printf("\n**** VENDA DE PRODUTO ****\n");
printf("Marca: %s\n", novoCarro->marca);
printf("Ano_de_Fabricação: %i\n", novoCarro->ano_de_Fabricacao);
printf("Cor: %i%%\n", novoCarro->cor);
printf("Preco: %.2f\n", novoCarro->preco);
printf("Valor de venda: %.2f\n", valorVenda);
}
float calcularValorVenda(CARRO* novoCarro) {
return (novoCarro->preco - (novoCarro->preco * novoCarro->desconto / 100));
}
int main() {
setlocale(LC_ALL, "Portuguese");
CARRO* ptr_carro = criarCarro();
//leitura dos dados
lerDados(ptr_carro);
float valorVenda = calcularValorVenda(ptr_carro);
//impressão dos dados da struct PRODUTO na tela
imprimirDados(ptr_carro, valorVenda);
return 0;
}
Which shows
Digite a MARCA do produto:
Aston Martin DB5
Digite o ANO_DE_FABRICACAO do produto:
1964
Digite a COR do produto:
Prata
Digite o PRECO do produto:
18000000
**** VENDA DE PRODUTO ****
Marca: Aston Martin DB5
Ano_de_Fabricação: 1964
Cor: Prata
Preco: 18000000,00
Valor de venda: 18000000,00
Pressione qualquer tecla para continuar. . .
preco float
is inverted. Usefloat preco;
Post a complete, compileable program.– arfneto
You stated
char cor[100]; preco float;
but does not make the reading compatible with this definitionprintf("Digite o COR do produto: \n"); scanf("%f",&novoCarro->cor); printf("Digite o PRECO do produto: \n"); scanf("%d",&novoCarro->preco);
– anonimo