I'm making a phonebook and I was trying to implement a system for not letting have repeated names on it.
After a long time trying to debug it, I found the reason why it wasnt working.
Code:
for(j=0; (x=fgetc(database))!=EOF; j++)
{
if (nl_c==4*nl_j)
{
if(x!=10)
{
name_db[j]=x;
printf("%d ", j);//dev
}
else
{
name_db[j]='\0';
j=0;
printf("j: %d\n", j);//dev
printf("%s", name_db);//des
getchar();//des
if((strcmp(name, name_db))==0)
{
printf("Já existe um contacto com o mesmo nome.!\n");
return 0;
}
nl_j++;
printf("nl_j:%d\n", nl_j);//dev
}
}
if(x==10)
{
nl_c++;
printf("nl_c:%d\n", nl_c);//dev
}
}
the database file goes like this:
Every thing goes fine, the only problem I have is when I try to reset the counter "j"it doens't reset. I mean it only changes to 0 inside the else statement.
I believe it's suposed to work that way when we change a variable inside a statement, but can't I just make it change the variable outside the statement just as well?
BTW, all those printf's were just for debugging purposes.
Thanks again and Merry Christmas,
EidgeAre