I try to write a code that you set a password and than set a limit of tries, which is the variable mistakes, and input different string to test if my guess and my password matches
The problem is, my variable, mistake, its value changes into "0" when the program goes into the for-loop even though I didn't change it's value in the loop.
my code is below
#include <stdio.h>
#include <stdlib.h>
int main()
{
char password[6];
gets(password);
int mistake = 0;
scanf("%d\n", &mistake);
char guess[6];
int i = 0;
int k = 0;
int count = 0;
printf("%d\n", mistake);
for(i = 0; i<mistake; i++)
{
gets(guess);
printf("%d", mistake);
for(k = 0; k<6; k++)
{
if(guess[k] != password[k])
{
if(i == mistake-1){
printf("you were electrocuted\n");
return 0;
}
else{
printf("wrong password\n");
break;
}
}
else
count++;
}
if(count == 6){
printf("correct\n");
return 0;
}
}
return 0;
}
I expect the value never changes inside the loop or out side the loop, however it's value always changes into 0. I check the variable, mistake, its value before the loop and inside the loop.I want to know why it changes and what should I do. I thought about if it's the problem about gets(), but I don't see anything that works for me.