8
I wanted a program that checks the user and password, if it was correct to display a message, otherwise I would repeat the check and if the attempts were equal to 3, it would display the limit message of reached attempts.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char user[20];
int senha, contador = 0;
printf("Entre com o usuario: ");
gets(user);
printf("Entre com a senha: ");
scanf("%d", &senha);
while((strcmp(user, "leonardo") == 0) && senha == 123) {
if(1) {
printf("\nAcesso concedido.\n");
break;
} else {
contador = contador + 1;
gets(user);
scanf("%d", &senha);
} if(contador == 3) {
printf("Limite de tentativas alcancadas.\n");
}
}
printf("Fim.\n");
return 0;
}
I’m having trouble with the loops.
When the program reaches 3 attempts it will end with both while and if
– leonardo
Yeah, I had thrown the counter to the end, then moved the
if
to the wrong location. I edited and now it’s ok.– Maniero
Only a small repair, attempts are 3, but the counter starts at 0, which makes the while and the check are 2
– Miguel
Yeah, it’s only 2 and still doesn’t show the message.
– leonardo
That’s right. Now.
– leonardo
Yes, the counter starts at 0, after all when it starts zero attempts form made. The
while
repeat until three attempts are reached. Actually the operator could not be<=
. Tidy.– Maniero
Goes from 0 to 2, is 3 attempts.
– leonardo