Like Tree1Likes
  • 1 Post By grumpy

What wrong with my copy strings program?

This is a discussion on What wrong with my copy strings program? within the C Programming forums, part of the General Programming Boards category; This code is actually an excerpt from my addressbook program.. address_count monitors the number of contacts added. Current_record monitors the ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    What wrong with my copy strings program?

    This code is actually an excerpt from my addressbook program..
    address_count monitors the number of contacts added.
    Current_record monitors the input's position in the array.

    Anyway, here it is.

    Code:
    void del_contact(int *address_count, int *current_record)
    {   
        int get, del, i, j;
        
            printf("\nEnter the number of entry/contact you want to delete.\n");
            printf("For example:\nIf you want to delete your first input type '1'\n");
            printf("Contact number:");
            scanf("%d", &get);
        
            del = get-1;
        
    
            //for loop to assign '/0' as the content for the given entry
            for(i=0; i<6; i++)
            {
                strcpy(addressbook[i][del], addressbook[6][0]); //address book[6][0] has no value
            }
            (*address_count)--; //since you're gonna delete an entry
            if((*current_record)>0)
                {
                    (*current_record)--;
                }
            
                for(i=0; i<6; i++)
                {
                    for(;get<=(*address_count-get);get++)
                    { 
                    strcpy(addressbook[i][get-1],addressbook[i][get]);
                    }
                }                
      }
    Code:
    for(i=0; i<6; i++)
                {
                    for(;get<=(*address_count-get);get++)
                    { 
                    strcpy(addressbook[i][get-1],addressbook[i][get]);
                    }
                }
    Here's my problem.
    This code is supposed to copy the strings from the next array to the array to be deleted. Which means that if there's an entry(addressbook) in arrays 1, 2 and 3 and I want to delete array 1, array 2 will go be copied to array 1 and array 3 to array 2 so that the program runs smoothly..

    But this doesn't work. D: any idea where I'm wrong? What should I do? Thanks a lot!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Your problems are on line 26. If you step through it with a debugger then the problem will become obvious.
    Pay attention to the value that "get" has, while single-stepping through those loops.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    How can I fix this? D:

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    You've been told where the problem is .... Do as advised, and step through the code with the debugger. That will help you understand the problem. Once you understand the problem, you will be able to fix it.

    No need to play helpless. We don't spoonfeed here.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-11-2013, 08:06 PM
  2. how to copy strings from mid way through
    By stephen101 in forum C Programming
    Replies: 4
    Last Post: 12-17-2010, 12:09 PM
  3. Replies: 4
    Last Post: 10-23-2010, 12:43 AM
  4. copy strings in array
    By davo666 in forum C Programming
    Replies: 17
    Last Post: 01-04-2009, 07:56 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21