I'm trying to create a program almost like "bank" using struct, but when the program should have read the string (variable "nome" that is name in portuguese) it totally ignore the "fgets" that I used. This is the part that I was talking about :
printf("\nNome: \n");
fgets(vet[cont+1].nome, sizeof(vet[cont+1].nome), stdin);
And I'm pretty sure that maybe the problem is with the dynamically allocation of my object array. Please, give me a help with this problem, thank you!
PS: I'm sorry but the code is in portuguese (my native language).
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*
Programa realiza uma alocacao dinamica por meio
de uma funcao que recebe a dimensao e retorna o vetor(ponteiro)
*/
struct CLIENTES
{
int ano_nasc, cpf[11];
float renda_m;
char nome[50];
}; //Lista de Objetos
int main(void)
{
//Declaracao de Variaveis
int cont=0, num, num_2, client, i, j;
CLIENTES *vet;
//Leitura de Dados
printf("Digite o numero de Clientes: ");
scanf("%d", &num);
vet = (CLIENTES*)malloc(num*sizeof(int));
printf("Digite os Dados do Cliente.");
while (cont != num)
{
printf("\nNome: \n");
fgets(vet[cont+1].nome, sizeof(vet[cont+1].nome), stdin);
printf("\nAno de Nascimento: ");
scanf("%d", &vet[cont+1].ano_nasc);
printf("\nCPF: ");
scanf("%d", &vet[cont+1].cpf);
printf("\nRenda Mensal: ");
scanf("%d", &vet[cont+1].renda_m);
cont++;
}
printf("\nDigite o numero do cliente que voce deseja conferir: ");
scanf("%d", &num_2);
for (i=0;i<num;i++)
{
if(num_2 == num)
{
printf("\nO que deseja saber sobre ele?\n");
printf("1-Nome\n2-Ano de Nascimento\n3-CPF\n4-Renda Mensal\n\n\n");
scanf("%d", &client);
if (client == 1)
{
printf("Nome: %c", vet[num_2].nome );
}
else if(client == 2)
{
printf("Ano de Nascimento: %d", vet[num_2].ano_nasc );
}
else if(client == 3)
{
for(j=0;j<11;j++)
{
printf("CPF: %d", vet[num_2].cpf[j]);
}
}
else if(client == 4)
{
printf("Renda Mensal: %f", vet[num_2].renda_m );
}
}
}
//Finalizando o Programa
printf("\n\nFim do Programa!");
getch();
return 0;
}
Problems that I see:
You are allocating the wrong amount of memory in the line:
That should be:
See Do I cast the result of malloc?. The answers explain why you should not cast the return value of
malloc
.You are using
fgets
after ascanf
.scanf
leaves the newline and other whitespace characters on the stream. Whenfgets
is called right after that,fgets
reads just the whitespace and the newline. You need to add code to ignore the rest of the line after the call toscanf
and before the call tofgets
.after that,
should read the data correctly.
You are using the wrong value in the line:
cpf
is an array onint
s. If you want to read just oneint
, you can use:You are using the wrong format specifier in the line:
It should be:
You are using the wrong index to access the array
vet
. Everywhere you usevet[cont+1]
, it should bevet[cont]
. By usingvet[cont+1]
, you are not using the first element of the array,vet[0]
, and accessing memory beyond what you allocated for when by accessingvet[num]
.If you fix the above problems, your program might work.