0
Good afternoon guys, I’m starting to learn C in college, I have a job to do where I must create a code to register 5 different books (name, author, publisher) and generate a code automatically. The code should have a menu for the user to choose the option you want (0 quit, 1 register new book, 2 view books already registered). Well, my problem is in item 2 of my menu, at the time to show the user the books already registered, if I register the books they appear normal, however if I choose the option to show the books already registered not having any book registered still appears memory garbage in 2 entries of my vector, the vector has size 5, the problem is occurring only in the entries 1 and 2, the entries 0, 3 and 4 are appearing "The entrance [x] is empty!" as it should.
I wonder if someone can help me fix this and explain to me why this is happening, and only in 2 entries, follows below my code.
Thanks des de ja.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>
struct produto
{
int codigo;
char nome[51];
char autor[51];
char editora[21];
};
int aleatorio ()
{
int i;
int x;
int codigo;
srand(time(NULL));
x = rand() % 9999;
codigo = x;
return codigo;
};
int main()
{
setlocale(LC_ALL, "Portuguese");
struct produto livro[5];
int opcao;
int tamanho;
int i;
int x;
int cod;
char name[51];
char author[51];
char publisher[21];
i = 0;
x = 0;
opcao = 3;
while(opcao != 0)
{
printf("\n\n\n");
printf("\t Escolha uma das opcoes: \n");
printf("\t 1 - Cadastrar novo livro \n");
printf("\t 2 - Visualizar livros cadastrados \n");
printf("\t 0 - Sair \n");
printf("\n\n\n");
scanf("\t %d", &opcao);
system("cls");
switch(opcao)
{
case 0: // Encerar
system("cls");
exit(0);
break;
case 1: // inserir novo cadastro
if (i == 5)
{
printf("\t Sistema de cadastro lotado. Não é possível armazenar mais informações! \n");
system("pause");
system("cls");
break;
}
else
{
printf("\t Insira o nome do Livro: ");
scanf("%s", &name);
strcpy(livro[i].nome,name);
printf("\n");
fflush(stdin);
printf("\t Insira o nome do Autor: ");
scanf(" %s", &author);
strcpy(livro[i].autor,author);
printf("\n");
fflush(stdin);
printf("\t Insira o nome do Editora: ");
scanf(" %s", &publisher);
strcpy(livro[i].editora,publisher);
printf("\n");
fflush(stdin);
cod = aleatorio();
livro[i].codigo = cod;
printf("\t O codigo do livro registrado e: %d \n \n", livro[i].codigo);
printf("\t Livro cadastrado com Sucesso! \n");
system("pause");
system("cls");
i++;
break;
}
case 2: // mostrar todas as entradas
for (x=0; x<=4; x++)
{
tamanho=strlen(livro[x].nome);
if(tamanho == 0)
{
printf("\t\t A entrada %d esta vazia! \n", x+1);
fflush(stdin);
}
else
{
printf("\n");
printf("\t Entrada %d", x+1);
printf("\t Livro: %s\n", livro[x].nome);
printf("\t Autor: %s\n", livro[x].autor);
printf("\t Editora: %s\n", livro[x].editora);
printf("\t Codigo: %d\n ", livro[x].codigo);
printf("\n\n");
}
}
system("pause");
system("cls");
break;
default:
printf("\n\n");
printf("\t Opcao invalida \n");
system("pause");
system("cls");
}
}
}
You have not initialized your array with name being a string of zero length, so by listing the entire array you will be picking up memory junk. Without initializing the entire array you can circumvent this by limiting the display only of the already registered items, which you control using the i variable.
– anonimo
Thanks for the answer. You said "You didn’t initialize your array with name being a string" could you explain it better? and could you explain to me how I do it? because I need to show that the position is empty, not just show those that have already been registered.
– conrado macedo
for (i=0; i<5; i++) book[i]. name[0] = ' 0'; or: for (i=0; i<5; i++) strcpy(book[i]. name, "");
– anonimo
Great! it worked !!! Thank you very much!!
– conrado macedo