简体   繁体   中英

Deleting entries from a char array

I'm creating a program that gathers a char array of max 10 characters. It then asks the user to enter a character. If the character is found, it will delete all entries in the array of that character and move the remaining characters in the array forward to remove all gaps.

This is the code I have currently:

for (int n = 0; n == 10; n++)
{
    int index(0);
    **while (text[index] != EOT)
    {
        if (text[index] == letter)
        {
            while (text[index] != EOT)
            {
                text[index] = text[index + 1];
                index++;
            }
        }
        else
            index++;
    }**
}

the code in bold (or with the ** between it* is currently working, and removes the FIRST instance of the character the user enters. So I decided to put a for loop around the whole while loop to make it repeat that code 10 times. Therefore as the input is limited to 10 characters it will (or should) work?

However it doesn't do anything anymore. It won't even remove the FIRST instance of the character and it is really baffling me. Can anyone see where I am going wrong?

It's c++ and i'm using Visual Studios 2013 by the way.

Thanks!

This control statement of the loop

for (int n = 0; n == 10; n++)

means that the loop will be executed never. You assigned zero to n and then said: "Execute the loop while n is equal to 10". But n is answering: " I am not equal to 10".:)

You could perform the task simpler by using standard algorithm std::remove

For example

#include <algorithm>
#include <cstring>
//...

*std::remove( text, text + std::strlen( text ), letter ) = '\0';

You should use a std::vector, it's easier for you here.

for(std::vector<char>::iterator it = vect.begin() ; it != vect.end() ; it++)
{
    if((*it) == letter)
    {
        vect.erase(it);
    }
}

Your problems are because you are using the same index variable to loop in two different places

for (int n = 0; n == 10; n++)
{
    int index(0);
    **while (text[index] != EOT)   // loop 1
    {
        if (text[index] == letter)  // loop 1
        {
            while (text[index] != EOT)  // loop 2
            {
                text[index] = text[index + 1];  // loop 2
                index++;               // loop2
            }
        }
        else
            index++;  // loop 1
    }**
}

change your code to

for (int n = 0; n == 10; n++)
{
    int index(0);
    while (text[index] != EOT)
    {
        if (text[index] == letter)
        {
            int index2(index);
            while (text[index2] != EOT)
            {
                text[index2] = text[index2 + 1];
                index2++;
            }
        }
        else
            index++;
    }
}

I'd suggest the following solution:

std::string text;
char charToBeRemoved;

text.erase (std::remove(text.begin(), text.end(), charToBeRemoved), text.end());

when you do your loop you check you variable n wrong.

for (int n = 0; n == 10; n++)

should be

for (int n = 0; n < 10; n++)

this will loop ten times.

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