Sex selector on if parole

Asked

Viewed 409 times

-1

How would you do in this code to catch the sex of the person and display on printf()?

I’ve seen many activities where in the condition of if, if he used too many numbers and hit me a question: And if it were with letters?

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

int main()
{
  char sex[5], nome[200], f, m;

  printf("\n Digite o seu nome: " ); //pegar o nome da pessoa
  scanf(" %[^\n]s", &nome);

  printf("\n Digite seu sexo f ou m: "); //pegar o sexo da pessoa
  scanf("%s", &sex);

   if(sex == m)
    {
        printf(" bem vindo Senhor %s\n", nome); // se for homem
    }

   if (sex == f) 
    {
       printf(" Bem vinda Senhora %s\n", nome); // sefor mulher
    }

  else
   {
       printf("\n ERRO! \n");
   }

getchar();  
return 0;   
}
  • You are receiving the value in a variable "sex" and comparing it with the variables "m" and "f" that has no value... You do not need these two variables. Compare to same string (sex == "f") and (sex == "m")

  • Defining variables is good practice, but if it is to use only in this context I agree with @Rodrigotognin

3 answers

1


In this case you only need one character, no more than this, as the code already shows. So it changes almost nothing.

#include <stdio.h>

int main() {
    char sex, nome[200];
    printf("Digite o seu nome: ");
    scanf(" %[^\n]s", nome);
    printf("\nDigite seu sexo f ou m:"); //pegar o sexo da pessoa
    scanf("%c", &sex);
    if (sex == 'm' || sex == 'M') printf("Bem vindo Senhor %s\n", nome);
    else if (sex == 'f' || sex == 'F') printf("Bem vinda Senhora %s\n", nome);
    else printf("\n ERRO! \n");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you want to deal with the word you already have one answer about this. Another example.

  • In Dev C++, the code worked, but when it came to "Type your sex f or m:" . It already goes straight to n ERROR! n; Getting like this: Type your name: DIEGO Type your sex f or m: // This part does not expect to type f or m ERROR!

  • But in the context of the question, your answer takes away exactly my doubt, although using the switch case, it would be smarter on my part, but as a learning experience, I learned a lot today!

  • Dev C++ is a pump not recommended by C or C++ professionals. Working is different than being right. https://i.stack.Imgur.com/i6eJF.jpg. has even a more intelligent way than this, but it is not the switch.

  • Truth about kkk image.

0

First of all

if(sex == m)
    {
        printf(" bem vindo Senhor %s\n", nome); // se for homem
    }

This comparison mode is wrong. Because the sex variable is a char vector, or rather, string as you want to call it. This comparison == is for numbers (integers, float...) or can use but m has to be like this’m' and sex has to specify the position of the vector to refer to the character itself; and its m and f are variables and have nothing in them, to use them it would be necessary to perform a scanf of m or f or assign a character f='f'...

You can use strcmp to compare for example:

if(strcmp(sex,'m')==0) 
{
printf(" bem vindo Senhor %s\n", nome); // se for homem
 }

Requirements:

To use strcmp you need to use the #include < string library. h > .

To improve your code:

scanf("%s", &sex);

You could use just one getch() or getche() since the input is only a letter;

Requirements: To use the getch or getche function it is necessary to include the conio. h library in the program;

0

You can do

char sex;

When you take the amount use scanf("%c",&sex) or scanf("%s",&sex) and to compare you use if(sex == 'm') use single quotes as it is only one character.

#include <stdio.h>

int main() {
    char sex;
    printf("Digite seu sexo f ou m:");
    scanf("%c", &sex);

    if (sex == 'm') printf("Masculino");
}

Browser other questions tagged

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