-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")
– Rodrigo Tognin
Defining variables is good practice, but if it is to use only in this context I agree with @Rodrigotognin
– Claudio Lopes