0
People I’m learning programming and I’m having doubts regarding the do-while loop, I was solving an exercise but at the time of executing the code presents a problem: does not end the loop, if I type the correct option it informs invalid option.
This is the code, I’m sorry for the mistakes, but as I said I’m learning and the only way I have to train is by cell phone, I don’t have a PC. If you find errors (I believe there should be several) I ask you to report these errors and explain to me what will be improved.
#include <stdio.h>
int main (){
int qtdf, qtdm, qtdp, qtdc, sexo, olhos;
// As variáveis abaixo tive que dar valor a elas pois o compilador "exigiu".
qtdf = 0;
qtdm = 0;
qtdp = 0;
qtdc = 0;
sexo = 0;
do {
printf ("Informe seu sexo:\n\n");
printf (" 1 - Feminino 2 - Masculino\n\n");
scanf (" %d", &sexo);
printf ("Informe a cor dos seus olhos:\n");
printf ("1 - Pretos 2 - Claros \n\n");
scanf (" %d", &olhos);
if (sexo == 1){
qtdf = qtdf + 1;
} else if (sexo == 2){
qtdm = qtdm + 1;
} else {
printf ("Opção inválida\n");
}
if (olhos == 1){
qtdp = qtdp + 1;
} else if (olhos == 2){
qtdc = qtdc + 1;
} else {
printf ("Opção inválida\n");
}
} while (sexo != 0);
printf ("Total de sexo feminino: %d\n", qtdf);
printf ("Total de sexo masculino %d\n", qtdm);
printf ("Total de olhos pretos: %d\n", qtdp);
printf ("Total de olhos claros %d\n", qtdc);
return 0;
}
!
Invalid option appears because you entered the value 0 for sex, IE, your gender condition whether it is male or female would not enter any of them, resulting in Else.
– Kevin Kouketsu
I tested the code here, the loop stops usually when 0 is typed in sex. And about the "invalid option" being printed, it’s for the reason that Kevin said above.
– Caio Henrique Reblin
To better understand, add a
else if(sexo == 0){printf ("Seu texto...\n");
above the firstelse
.– Caio Henrique Reblin