Problem with Struct in C

Asked

Viewed 1,405 times

1

In that code:

#include <stdio.h>
#include <stdlib.h>

//Usando o struct(Estruturas)
  struct cadastro {
    char nome[50];
    int idade;
    char rua[50];
    int numero;
    };
 int main()
 {
     printf("Digite suas informacaoes:\n");
     struct cadastro c;

     printf("Digite seu nome:\n");
     //Le do teclado uma string e armazena no campo nome
     gets(c.nome);

     printf("Digite sua idade:\n");
     //Le do teclado um valor inteiro e armazena no campo idade
     scanf("%d", &c.idade);

     printf("Digite o nome da sua  rua:\n");
     //Le do teclado uma string e armazena no campo rua
     gets(c.rua);

     printf("Digite o numero da rua:\n");
     //Le do teclado um valor inteiro e armazena no campo numero
     scanf("%d", &c.numero);

     system("cls");

     printf("Nome: %s\n", c.nome);
     printf("Idade: %d\n", c.idade);
     printf("Rua: %s\n", c.rua);
     printf("Numero: %d\n", c.numero);

     system("pause");
     return 0;
 }

I can’t type the street name with the scanf, it jumps right to the reading of the int number. In the printf he does not inform the street, I can not solve. inserir a descrição da imagem aqui I removed the system("cls")(clear the screen) to show.

  • You can exchange gets(c.rua) for fgets(c.rua, 50, stdin) and test?

  • Yeah, looking at your code, I’m not sure if the problem is in the gets(c.rua) or the scanf above. You can test your code without the lines in which you question age? Test with the name question and then the street question, one followed by the other. See what happens.

  • Because it is expensive, the problem is not in gets(), but in scanf("%d", &c.age). As if the "enter" that is given at that time of execution affects the execution of gets(). Test this here, please: , instead of scanf("%d", &c.idade), put the two lines: scanf("%d", &c.idade); getc(stdin);

2 answers

3

Actually the biggest problem is - beyond gets - in want to "capture" the \n. With the example Yonathan is solved, but one cannot catch names with space. To capture names with spaces and not having this problem put before each form of scanf capture a space. Ex: scanf(" %[^\n]", c.rua); <- space before the %

Mainly because the capture of numéros with integers also "takes" the whole and leaves the \n leftover. Code to read with strings without problems:

#include <stdio.h>

    struct cadastro {
    char nome[50];
    char rua[50];
    int idade;
    int numero;
};

int main(void) {

    struct cadastro c;

    printf("Digite suas informacaoes:\n");

    printf("Digite seu nome: ");
    scanf(" %[^\n]", c.nome);

    printf("Digite sua idade: ");
    scanf(" %d", &c.idade);

    printf("Digite o nome da sua rua: ");
    scanf(" %[^\n]", c.rua);

    printf("Digite o numero da rua: ");
    scanf(" %d", &c.numero);

    printf("\nNome: %s\n", c.nome);
    printf("Idade: %d\n", c.idade);
    printf("Rua: %s\n", c.rua);
    printf("Numero: %d\n", c.numero);

    return 0;
}

A space on the third would suffice scanf, but put in all for precautions. If you need to use the function

void clearStdin(void){  

    int c;

    while(( c = getchar() ) != '\n' && ( c != EOF ));
}

to remove any flow that is still accumulated in the inlet, causing it to be discarded. Just by the function after any output, mainly names with predetermined size.

Example:

#include <stdio.h>

struct cadastro {
    char nome[50];
    char rua[50];
    int idade;
    int numero;
};

void clearStdin(void){  

    int c;

    while(( c = getchar() ) != '\n' && ( c != EOF ));
}

int main(void) {

    struct cadastro c;

    printf("Digite suas informacaoes:\n");

    printf("Digite seu nome: ");
    scanf("%49[^\n]", c.nome);
    clearStdin();

    printf("Digite sua idade: ");
    scanf("%d", &c.idade);

    printf("Digite o nome da sua rua: ");
    scanf(" %49[^\n]", c.rua);
    clearStdin();

    printf("Digite o numero da rua: ");
    scanf("%d", &c.numero);

    printf("\nNome: %s\n", c.nome);
    printf("Idade: %d\n", c.idade);
    printf("Rua: %s\n", c.rua);
    printf("Numero: %d\n", c.numero);

    return 0;
 }

Because if the user type something larger than 49 characters the rest will be discarded.

1


So, some things you need to fix, and the program I’m going to post works. If you want to read spaces need to adjust, I did it fast now and will leave you some links to fix this "little problem" in C.

#include <stdio.h>
#include <stdlib.h>

struct cadastro {
    char  nome[50];
    char rua[50];
    int idade;
    int numero;
};

struct cadastro c;

int main(int argc, char** argv) {

    printf("Digite suas informacaoes:\n");

    printf("Digite seu nome: ");
    scanf("%s", &c.nome);

    printf("Digite sua idade: ");
    scanf("%d", &c.idade);

    printf("Digite o nome da sua rua: ");
    scanf("%s", &c.rua);

    printf("Digite o numero da rua: ");
    scanf("%d", &c.numero);

    printf("\nNome: %s\n", c.nome);
    printf("Idade: %d\n", c.idade);
    printf("Rua: %s\n", c.rua);
    printf("Numero: %d\n", c.numero);

    return 0;
}

http://www.vivaolinux.com.br/topico/C-C++/Ler-nomes-com-espaco http://blog.lucaspolo.com.br/2010/07/lendo-string-com-espacos-na-funcao.html

  • No, the gets shouldn’t be used and the program had things it didn’t need/shouldn’t. This leaner, just pick there the code that fits best for reading the string in the scanf and will be ready.

Browser other questions tagged

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