can't get duplicate delay

This is a discussion on can't get duplicate delay within the C++ Programming forums, part of the General Programming Boards category; the program runs, but it does not accomplish the duplicate-delete in the array. for example, if the array is 4 ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    can't get duplicate delay

    the program runs, but it does not accomplish the duplicate-delete in the array. for example, if the array is 4 and someone enters "bill", it should output "bil". can anyone figure out what's wrong?

    Code:
    #include<iostream>
    
    using namespace std;
    void delete_repeats(char a[], int& index_used);
    void erase_and_shift(char a[], int& index_used, int& arraysize);
    
    
    
    int main ()
        {
         int index_used = 0;
         int arraysize = 0;
         char a[100]; 
    
     
            cout<<"Please enter the size that you would like of an array from 0-100 : \n";
            cin >>arraysize;
    
            cout<<"Now enter "<< arraysize<<" characters :\n";
        
    
            int k= 0;
            
            while (k<arraysize)
            
                {
                cin>> a[k];
                k++;
                }
    
           	 delete_repeats(a, index_used);
    
    
           cout <<"The numbers you entered are: ";
            
          
    	   for (int index = 0; index < arraysize; index++)
    	   {
    			
    
    		   cout << a[index];
    	   }
        cout << endl;
    	     
        
        return 0;
    
        }
    
    
    
    void delete_repeats(char a[], int& index_used)
    
    {
    	int i, j;
    
    	for(i = 0; i < index_used - 1; i++)
    		for(j = index_used - 1; j > i; j--)
    			if(a[j] == a[i])
                {
    			erase_and_shift(a, j, index_used);
                }
    }
    
    void erase_and_shift(char a[], int& index_used, int& arraysize)
    {
    //int k = index_used
       
        int k = arraysize;
        //while k < arraysize increments of k by +1
     
        while (k < index_used -1)
            {
            k++;
            }
            //array of k = array of k +1 increment of -1
        a[k]= a[k+1];
        index_used --;
       
        }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    It looks like when you call delete_repeats, you pass index_used which is 0. Then you have a for loop that runs while i is less than -1 (which is index_used - 1), when i starts at 0.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    i tried editing the values in my delete repeat function to match at -1 and 0, but i still couldn't get a pleasing output. what is it that i'd be editing ?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I'm not sure how you'd fix it, all I know is that the for loop will never run since 0 is never less than -1.

    What were you intending to happen? Are you sure you wanted index_used there?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    the function goes through the array individually and checks if each byte char/int equals the next, if so it deletes it and moves the arrays down 1.

    i.e.

    Enter: AABC

    Output: ABC

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I know what the end result is supposed to be. I'm asking what that specific code is supposed to do. What is the goal of the first for loop in delete_repeats?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    the first loop in delet repeats tests the array for duplicates. saves the byte and tests against others to see if duplicate exists.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Could you be more specific? Try writing at least 10 sentences, feel free to write more. My point is that the problem is in the for loop. It never runs because 0 is never less than -1. Do you see that? If you can explain the details of what each variable is supposed to do and what the goal of each line of code is (in fact, what the goal of each word of code in that function is) that would help us and you. You need to understand what your code is doing and attempting explanation is a great way of helping along understanding. I'm not talking about an overview, I'm talking about specifics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding delay in the connection
    By byatin in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-19-2008, 02:59 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-05-2005, 11:23 AM
  4. duplicate detection algorithm
    By Gustaff in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 11:26 AM
  5. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:28 AM

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