I am having some trouble with this program ..
if input is abaabcdbab output should read abcd.... program is supposed to delete the repeated characters and output only one of each character but it is not working .... and ideas would be welcome

//Reads the letters in an array and deletes all repeated letters.
//Requires a function called deleteRepeats, also a partially filled
//array that requires two arguments. The function will have two formal parameters:
//an array parameter and a formal parameter of type int that gives the number of array positions.

#include <iostream>
using namespace std;
const int DECLARED_SIZE = 10;

void fillArray(char a[], int size, int& numberUsed);

void sort(char a[], int numberUsed);

void deleteRepeat(char a[], int& n);

int indexOfFirst(const char a[], int startIndex, int numberUsed);

int main()
{
    cout << "This program sorts characters and deletes any repeated characters.\n";

    int size = 4, numberUsed;
    char a[10];

    fillArray(a, 10, numberUsed);
    sort(a, numberUsed);
    deleteRepeat(a, size);

    cout << "In sorted order the letters are:\n";
    for (int index = 0; index < numberUsed; index++)
        cout << a[index] << " ";
    cout << endl;

    return 0;
}

void fillArray(char a[], int size, int& numberUsed)
{
    cout << "Enter up to " << size << " letters or characters.\n"
         << "Mark the end of the list with a negative number.\n";
    int next, index = 0;
    cin >> next;
    while ((next >= 0) && (index < size))
    {
        a[index] = next;
        index++;
        cin >> next;
    }

    numberUsed = index;
}

void sort(char a[], int numberUsed)
{
    int indexOfNext;
    for (int index = 0; index < numberUsed - 1; index++)
    {
        indexOfNext = indexOfFirst(a, index, numberUsed);

    }
}

void deleteRepeat(char a[], int& n)
{
    int i,j,k;

    for(i=0; i<n-1; i++)
        for(j=i+1; j<n; j++)
            if(a[i]==a[j])
            {
                for(k=j; k<n; k++)
                {
                    a[k]=a[k+1];
                        n--;
                        j--;
                }
            }
}

int indexOfFirst(const char a[], int startIndex, int numberUsed)
{
    int min = a[startIndex],
        indexOfMin = startIndex;
    for (int index = startIndex + 1; index < numberUsed; index++)
        if (a[index] < min)
        {
            min = a[index];
            indexOfMin = index;
        }
    return indexOfMin;
}

Recommended Answers

All 3 Replies

For starters, your input function can't make up its mind what it's supposed to do.

void fillArray(char a[], int size, int& numberUsed)
{
   cout << "Enter up to " << size << " letters or characters.\n"
      << "Mark the end of the list with a negative number.\n";
   int next, index = 0;
   cin >> next;
   while ((next >= 0) && (index < size))
   {
      a[index] = next;
      index++;
      cin >> next;
   }

   numberUsed = index;
}

You're taking input into an int variable, but you've told the user to enter "letters or characters". Letters are a subset of character, namely a-z and A-Z. Characters are all things you can press on the keyboard, and a few other things. When you enter a letter for that input statement, the input fails, as it's expecting a numeric input. Your loop exits, and the function returns, have read nothing in.

If you want letters, make the input variable type char. Then you'll need a different way to end the loop besides a negative number.

A common way to handle this is a loop controlled by both the limit of the input array and the success of the input action. Use of control-z at the keyboard (control-D in *nix) will simulate end of file action, so this method will also be useful reading from files.

char input;
int count = 0;
while ( count < limit && cin >> input )
{
      // do stuff
      count++;
}

Also, when you post code, please use the code tags correctly. Don't use quote block, just the code tags, like this:

[code]

your code goes here

[/code]

Get that working, then we'll look at your sort and deletion parts.

thanks for the help ...

decrement of n and j should be outside of for loop for k. in the if statement.

void deleteRepeat(char a[], int& n)
{
    int i,j,k;
    for(i=0; i<n-1; i++)
        for(j=i+1; j<n; j++)
            if(a[i]==a[j])
            {
                for(k=j; k<n; k++)
                {
                    a[k]=a[k+1];

                }n--;
                 j--;
            }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.