简体   繁体   中英

C++:array functions

How can I write a program that reads in, a collection of characters from the key board and outputs them to the console. Data is input at random, but output selectively. Only unique characters are displayed at the console. Therefore, every character should be displayed once, no matter how many times it appears in the array.

For example, if an array

Char letterArray[ ] = {B,C,C,X,Y,U,U,U};

The output should be:

B,C,X,Y,U

This is what I have done so far...

char myArray [500];
int count = 0;
int entered = 0;
char num;

while (entered < 8)
{
    cout << "\nEnter a Character:";
    cin >> num;

    bool duplicate = false;
    entered++;

    for (int i = 0; i < 8; i++)
    {
        if (myArray[i] == num)
            duplicate=true;
    }

    if (!duplicate)
    {
        myArray[count] = num;
        count++;
    } // end if
    else
        cout << num << " character has already been entered\n\n";

    // prints the list of values
    cout<<"The final Array Contains:\n";

    for (int i = 0; i < count; i++)
    {
        cout << myArray[i] << " ";
    }
}

I believe you could make use of std::set<> .

" Sets are a kind of associative container that stores unique elements <...> elements in a set are always sorted from lower to higher following a specific strict weak ordering criterion set "

It would be much more efficient to create an array of size 128 (assuming you are dealing with ASCII) that is initialized with false . Every time you get a character, check its ASCII value and if the array is true on that value you don't print it. After that, update the value of the array on the character value to true. Something like:

bool *seenArray = new bool[128]();

void onkey(char input) {
    if(((int)input) < 0) return;
    if (!seenArray[(int)input]) {
         myArray[count] = input;
         count++;
         seenArray[(int)input] = true;
    }         
}

Looking through your code...

char myArray [500];

Why 500? You never use more than 8.

char num;

Confusing naming. Most programmers would expect a variable named num to be a numeric type (eg int or float ).

while (entered < 8)

Consider replacing 8 with a constant (eg const int kMaxEntered = 8; ).

cin >> num;

cin might be line-buffered; ie it does nothing until a whole line is entered.

for (int i = 0; i < 8; i++)
{
    if (myArray[i] == num)
        duplicate=true;
}

You're accessing uninitialized elements of myArray . Hint: your loop size should not be 8.

Consider using continue; if you find a duplicate.

if (!duplicate)
{
    myArray[count] = num;
    count++;
} // end if
else
    cout << num << " character has already been entered\n\n";

Your // end if comment is incorrect. The if isn't ended until the else is done.

You may want to add braces around the else clause, or remove the braces from the if clause by combining its two lines into the one-line myArray[count++] = num; .

// prints the list of values
cout<<"The final Array Contains:\n";

for (int i = 0; i < count; i++)
{
    cout << myArray[i] << " ";
}

You're printing the list every time you get a single input?

Don't use \\n in text to cout unless you specifically want to micromanage buffering. Instead, use endl . Also, always put spaces around binary operators like << and don't randomly capitalize words:

cout << "The final array contains:" << endl;
for (int i = 0; i < count; i++)
    cout << myArray[i] << " ";
cout << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM