1
Good evening, everyone.
I’m having trouble with a simple code for a college job where I can’t figure out why the bug is wrong, since Dev-C++ does not report any errors. To prevent someone from my college from finding this topic by chance and copying my code, I will use an example from the teacher himself, with the addition of the one that is giving error in mine:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAM 3
typedef struct{
int cod;
char nome[50];
char telefone[20];
} tipo_pessoa;
int main(){
tipo_pessoa lista[TAM];
int i=0;
for(i=0; i<TAM; i++){
printf("Insira o nome da pessoa %d:\n", i+1);
gets(lista[i].nome);
fflush(stdin);
printf("Insira o telefone de pessoa %d (XX XXXXX XXXX):\n", i+1);
gets(lista[i].telefone);
fflush(stdin);
lista[i].cod = i+1;
}
system("cls");
printf("Os cadastros foram preenchidos...\n\n");
system("pause");
for(i=0; i<TAM; i++){
printf("%s - %s - %s\n", lista[i].cod, lista[i].nome, lista[i].telefone);
}
}
My problem is with lista[i].cod
, when trying to "print" on the screen, the program finishes on time. If I take the lista[i].cod
of printf
, the code runs normally. If I ask the user to specify the number, it returns a strange character, but still, I need the code to be automatically generated by system
and I need it to be shown on printf
.
What am I doing wrong?
I’m sorry if it’s something stupid I’m not seeing, but I’ve been banging my head on this for four hours.
Dev-C++ is a dinosaur, has long been abandoned. There are many other Ides for more modern C development.
– Victor Stafusa
Never use the function
gets
. NEVER!– Victor Stafusa