Using Struct in Exercise C Language

Asked

Viewed 5,625 times

3

/* Write an algorithm that reads data from "N" people (name, sex, age and health) and indicate whether you are fit or not to fulfill the mandatory military service. Inform the totals. */

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

struct Pessoa
{
    char nome[30];
    char sexo[1]; // M = Masculino e F = Feminino
    int idade;
    char saude[5];
};

main()
{

    int quant; // Quantidade de pessoas

    printf("Digite a quantidade de pessoas: ");
    scanf("%d", &quant);

    struct Pessoa candidato[quant];

    int i;

    for(i = 0; i < quant; i++)
    {
        printf("\nNome: ");
        fgets(candidato[i].nome,30,stdin);

        printf("\nSexo: ");
        fgets(candidato[i].sexo,1,stdin);

        printf("\nIdade: ");
        scanf(" %d", &candidato[i].idade);

        printf("\nSaude: ");
        fgets(candidato[i].saude,5,stdin);

        if(candidato[i].idade >= 18 && candidato[i].saude == "boa")
        {
            printf("O %s, sexo %s com idade %d e com saude %s esta apto ao servico militar. \n\n",
                   candidato[i].nome, candidato[i].sexo, candidato[i].idade, candidato[i].saude);
        }
        printf("\n\n");

    } // fim do for

} // fim função main()

But when I use the fflush shows "Name: Sex" (in the same line) how do I resolve this?

  • What is your doubt?

  • The fflush(stdin) is defined in Windows. Other systems do not work. If you want a portable solution you should try to find another solution.

  • When I use fflush does not show type Name: Sex (in the same line) how do I resolve it then?

  • The simplest solution will be to use fgets() for all inputs. Then, in specific cases, convert the string to integer with sscanf().

  • fgets() codeblocks does not work. :(

  • 1

    There is no point in you declaring sex[1];. When you declare a string you need to predict the terminator character ('0') and if you only predict the 1 size then there is no space for it. Note that char sex; (the variable has space for a character) is quite different from char sex[1]; (a string of length 1). When doing gets(candidate[i]. sex); you will put the ' 0' out of the allocated area for the variable. Use char sex; and scanf("%c", &sex);.

  • In the future, when you have a similar question, do not forget to beg what error you are making, what input you are going through, what exit you wanted, etc. It is much more difficult to give a good answer if you just say that "it is not working right"

  • If fgets did not work it is because you made some mistake in its use, probably erroneously specified its parameters. See details in: http://pubs.opengroup.org/onlinepubs/000095399/functions/fgets.html

  • I’m sorry, I’ve been busy with other things. But I’m still unsuccessful in this program, keeps showing the three questions at once. Name: Sex: age in the same line.

Show 4 more comments

3 answers

4


  1. struct definition is better outside of function.

  2. char sexo[0] has space for zero letters. To store "M" (or "F") and the terminator needs at least 2 characters

  3. for(i = 0; i <= quant; i++) will run the cycle a more than necessary Vaz

  4. After picking up age with the scanf(), a ENTER is "hung" in the input stream. The next reading takes that ENTER and returns an empty string

  5. Where’s the rest of the program? ???

  6. Function gets() should not be used. Because this function was so poorly thought out it was no longer part of the C Standard in the last version (few compilers use the latest version of the Standard)

  7. Good luck and have fun!

  • Thanks "pmg", but the program is just that. The sex[0] char is only for M or F. Pq o for vai da + 1 loop? I did not understand about the scanf of age although in it that ta giving pal even. If not use the use gets what then?

  • I was referring to the "Report totals". char sexo[0] has space for zero characters; the string "M" 2 characters. Imagine that quant is 2; the i begins in 0, afterward 1, afterward 2, and then ends: 3 times!! Instead of the gets() I advise the fgets(). the use is similar to that of the gets(); You just need to watch out for the final ENTER.

  • I understood about the loop and fix, so sex[1] has to be like this? I’ve changed the function to fgets() too.

  • 1

    @phpricardo In C strings are null-terminated (\0). That’s why you need at least 2 characters: one for the letter itself ("M" or "F") and another for the terminator. If you only use one, string-like functions will write outside the memory region of your variable, then anything can happen... (in general, bad thing :P)

  • Thanks for the explanation. ;)

0

Try (note dynamic memory allocation):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Pessoa {
    char nome[31];
    char sexo; // M = Masculino e F = Feminino
    int idade;
    char saude[6];
};

main() {
    struct Pessoa *candidato;
    int quant; // Quantidade de pessoas
    int i;
    printf("Digite a quantidade de pessoas: ");
    scanf("%d", &quant);
    candidato = (struct Pessoa *) malloc(quant*sizeof(Pessoa));
    for(i=0; i<quant; i++)  {
        printf("\nNome: ");
        fgets(candidato[i].nome,30,stdin);
        printf("\nSexo: ");
        scanf("%c",&candidato[i].sexo);
        printf("\nIdade: ");
        scanf(" %d", &candidato[i].idade);
        printf("\nSaude: ");
        fgets(candidato[i].saude,5,stdin);
        if(candidato[i].idade >= 18 && strcmp(candidato[i].saude, "boa") == 0) {
            printf("O %s, sexo %c com idade %d e com saude %s esta apto ao servico militar. \n\n",
                   candidato[i].nome, candidato[i].sexo, candidato[i].idade, candidato[i].saude);
        }
        printf("\n\n");
    } // fim do for
    free(candidato);
} // fim função main()

-2

One option would be to use instead of fgets the command scanf:

    scanf(" %[^\n]s",&candidato[i].nome);

Browser other questions tagged

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