Form with Struct and Pointers

Asked

Viewed 84 times

0

Guys, I made this program to receive name, address and phone of 2 people, he receives the data of the first people quietly, problems begin with the data of the second person, the name and address of the second person he receives, but the program simply skips the entrance of the phone field and goes to the next step, which is to print the information on the screen, the printing for the data of the first registration happens normally, but for the second person, it prints only the name followed by a number in place of the address that is always equal in all tests (32767), after that the program finishes, wanted to understand why this happens, I have checked the code several times and did not find the error.

#include <stdio.h>
#include <string.h>

struct Pessoas
{
        char nome[30];
        char endereco[50];
        int telefone;
};

int main ()
{

        int i;
        void print ();
        void recebe ();

        struct Pessoas pessoas[2];

        for (i=0; i!=2; i++)
        {
                recebe(&pessoas[i]);
        }
        printf ("\n\n");
        for (i=0; i!=2; i++)
        {
                print (&pessoas[i]);
        }
        return 0;
}


void recebe (struct Pessoas *y)
{
        fflush (stdin);
        fgets (y->nome, 30, stdin);
        fgets (y->endereco, 50, stdin);
        scanf ("%d", &y->telefone);
}

void print (struct Pessoas *x)
{
        printf ("%s", x->nome);
        printf ("%s", x->endereco);
        printf ("%d", x->telefone);
}
  • only one detail, the command forI think it would look better this way: for (i=0; i<2; i++), pq you are starting at zero and incrementing 1 in 1

2 answers

0

void recebe (struct Pessoas *y)
{
        fflush (stdin);
        fgets (y->nome, 30, stdin);
        fgets (y->endereco, 50, stdin);
        scanf ("%d", &y->telefone);
}

Don’t mix fgets() and scanf()

void recebe (struct Pessoas *y)
{
        fgets (y->nome, 30, stdin);
        fgets (y->endereco, 50, stdin);
        char tmp[99];
        fgets(tmp, 99, stdin);
        sscanf (tmp, "%d", &y->telefone);
}

Remember that the fgets() retain the ENTER. You might want to take it out for the name and address.

-1

void recebe (struct Pessoas *y)
{
        fflush (stdin);
        fgets (y->nome, 30, stdin);
        fgets (y->endereco, 50, stdin);
        scanf ("%d", &y->telefone);
}

Try to receive the data entries as follows:

scanf(" %30[^\n]s ",&nome);
scanf(" %50[^\n]s ",&endereco);
scanf ("%d", &y->telefone);
  • 30 ==> 29 and 50 ==> 49 and’s' is higher!

  • when using " %50[^\n]s", the initial space disregards any type of space or line break typed in the scanf, %50 I identify that this scanf can catch up to 50 char [^\n] ignores any space or line break typed in that scanf

  • it may be that it is identifying some space or line break and that the part of phone is ignored, so I sent this code, this same problem in the project that I was doing, and this line of code solved me.

  • My idea was to remember that scanf("%3s", p) can write 3 characters besides the '\0'. If p is defined as char p[4]; no problem; if defined as char p[3]; may occur buffer overflow.

  • Oh! The "s" that’s too much is this: %[^ ]s.

  • The syntax for receiving a string via scanf() is:scanf(“%s”, nome_da_string);

Show 1 more comment

Browser other questions tagged

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