Thread: Checking for repeated data: variable against file

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    18

    Checking for repeated data: variable against file

    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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is this Italian or Portuguese? >> Já existe um contacto com o mesmo nome.

    The way to check for duplicate names is to sort the names, then use a binary, or simple search, to search for duplicate names.

    The names can be sorted by actually moving the names in the array, or by not moving the names and sorting them through an index of integers or pointers.

    Having them sorted, means searches can be very fast, using a simple binary search


    What does this do?

    Code:
    if (nl_c==4*nl_j)  //??
    I hope that doesn't mean name length of c == 4*name length of j. That sounds very dodgy.

    Please post up all the specifics, and a 3 line sample input data file, and let's take a look at making this a good personal phone book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 07:08 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM