List item causing error in C program

Asked

Viewed 95 times

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.

  • Never use the function gets. NEVER!

2 answers

1

printf("%d - %s - %s\n", lista[i].cod, lista[i].nome, lista[i].telefone);

lista[i].cod is a whole, so you have to put a %d to indicate that an integer will be written and not a %s of "string".

1


Use %d instead of %s to print numerical codes:

 printf("%d - %s - %s\n", lista[i].cod, lista[i].nome, lista[i].telefone);

That is, the first %s was to be a %d.

Also, never use the function gets. Instead:

gets(lista[i].nome);

Use this:

fgets(lista[i].nome, 50, stdin);

Instead:

gets(lista[i].telefone);

Use this:

fgets(lista[i].telefone, 20, stdin);

Reasons not to use gets I’ll explain in this answer and I also talk about her in this other.

Also be careful with the fflush(stdin). See more about this in that reply and in that other one as well.

Finally, Dev-C++ is a very old and long-abandoned ide. Look for another ide for more modern C, such as Code::Blocks, Netbeans, Visual Studio, etc.

  • I use Visual Studio for other languages, only using Dev-C++ for this work specifically to use the gets and etc. I know his problems, but that’s what the teacher is using now at the beginning of the module, and he asks this in the statement. And for once, I’ve made a stupid mistake regarding %d and %s, unfortunately.

  • @Maxpresi Pedir gets in the statement is to kill. This function drug should never have been invented, so asking students to use it is teaching wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.