Good morning,
I wanted to know how I can optimize buffer cleaning when using the getchar () function, as an example follow the following codes:
I get the following code from the correct output using the "scanf spacing" technique as shown:
#include <stdio.h>
int main()
{
float salario, imposto = 0.0;
char sexo;
printf ("Introduza o Salário: ");
scanf("%f",&salario);
printf ("Introduza o sexo: ");
scanf (" %c",&sexo);
switch (sexo)
{
case 'f':
case 'F': imposto = 0.10;
break;
case 'm':
case 'M': imposto = 0.05;
break;
}
printf("Imposto %.2f\n",salario*imposto);
return 0;
}
However I can not correctly run the following code:
#include <stdio.h>
int main()
{
float salario, imposto = 0.0;
char sexo;
printf ("Introduza o Salário: ");
scanf("%f",&salario);
printf ("Introduza o sexo: ");
sexo = getchar();
switch (sexo)
{
case 'f':
case 'F': imposto = 0.10;
break;
case 'm':
case 'M': imposto = 0.05;
break;
}
printf("Imposto %.2f\n",salario*imposto);
return 0;
}
Presenting the following output without letting me enter the sex:
Enter Salary: 100 Enter gender: Tax 0.00
Thanks for the help.