Hello,
I need to wwrite a function that will go through a dynamic character array that the user inputs then take it and delete the repeated characters. My program partially works but it seems as though whenever it deletes a set of repeated characters then it just stops rather than go through the whole char array. Here is my code:
#include<iostream>
#include<cstdlib>
using namespace std;
typedef char* CharArrayPtr;
void input(char sentence[], int& size);
void delete_repeats(char sentence[], int& size);
int main()
{
int size;
CharArrayPtr array;
array = new char[81];
input(array,size);
delete_repeats(array,size);
cout << "Resulting Array: ";
for(int i = 0; i < size; i++ )
{
cout << array[i] << " ";
}
cout << endl << endl;
delete [] array;
system("pause");
return 0;
}
void input(char sentence[], int& size)
{
int walker = 0;
cout << "Please enter a string of characters: ";
do
{
cin.get(sentence[walker]);
walker++;
}while (sentence[walker - 1] != '\n');
size = walker - 1;
}
void delete_repeats(char sentence[], int& size)
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (sentence[i] == sentence[j])
{
char *sentence = new char[size]; //allocate a new array of 'size' chars
delete [] sentence; // deallocate that array
sentence = new char[size - 1]; // allocate new array with 'size - 1' chars
j--;
size--;
for(int k = 0; k < size; k++)
cout << sentence[k];
cout << endl;
}
}
}
}