Array Shifting

I am trying to allow the user to enter up to 26 chars in an array, and then it will go through the array, delete all the duplicate chars, and then shift everything to the right. I can load the array, but i cant seem to get the delete/shift process to work. I am having all of my troubles with the deleteRepeats. I have tried many things with varying degrees of success... below is what i have so far. Any help would be great. Thanks.

#include <iostream>
using namespace std;
void enterArray(char a[], int size, int& numberUsed);
// Size is the max size of array a
//Number Used is number of values stored in a
//a[0] - a[numberUsed-1] are all char
void deleteRepeats(char a[], int numberUsed);
int main()
{
const int max_char = 26;
char sampleArray[max_char];
int numberUsed;
enterArray(sampleArray, max_char, numberUsed);
deleteRepeats(sampleArray, numberUsed);
for (int index = 0; index < numberUsed; index++)
cout << sampleArray[index] << " ";
cout << endl;
return 0;
}


void enterArray(char a[], int size, int& numberUsed)
{
cout << "Please enter up to " << size << " characters\n" << "End the statement with a capital 'Z'\n";
char next;
int index = 0;
cin >> next;
while ((next != 'Z') && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
numberUsed = index;
}

void deleteRepeats(char a[], int numberUsed)
{
int i = 0, y=1, x = 0, counter = 0;
for (int x = 0; x < (numberUsed - 1); x++)
{
for (int y = 1; y < (numberUsed); y++)
{
while(a[x] == a[y])
{
for(int i = x; i <(numberUsed -1); i++)
{
a[i] = a[i+1];
counter++;
}
a[counter] = 0;
counter = 0;
}

}
}
}

I actually it would shift everything to the left. I envision it would work like...

user enters:
a,a,a,a,s,d,a

and it would run though in various steps...
a,a,a,s,d,a,a
a,a,a,s,d,a
a,a,s,d,a,a
a,a,s,d,a
a,s,d,a,a
a,s,d,a
s,d,a,a
s,d,a
Then it would output the s,d,a at the end.
sorry for the bump, still having trouble and this is due today...
you need 2 nested for loop here and you have shift items in the array, like
1
2
3
4
5
6
7
8
9
10
for( int i = 0 ; i < length-1 ; i++)
  for( int j = i+1 ; j < length ; j++)
        if( a[i] == a[j] )
        {
               //shift
               for( int k = j ; k < length-1 ; k++)
                        a[k] = a[k+1];
               length--;
               j--;
        }

I didn't test the code above.
But i suggest you to use STL duplicate function.I bet , it works much efficient than this way.
Last edited on
That worked great. All i had to do was tack a a[length] = 0; on after the j-- and it worked amazingly. Thanks!
Topic archived. No new replies allowed.