Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly remove a character from a char array (with or without converting to a string)?

Sorry in advance for any possible duplicate question. I have been Googling a solution for my compiler error, for a week now, tried different workarounds from various answers around here, yet I keep getting some errors.

I am currently learning C++, trying to build a program that does basic stuff like vowel/consonant count, letter removal etc. Everything works fine until I get to the custom letter removal part. Basically, it's near impossible to do that using character functions (according to my knowledge), while converting to a string seems to spawn other kinds of errors.

Here is the code fragment where I keep getting errors:

if (strcmp (service, key4) ==0)
{
string str(s);

cout<<endl<<"Please insert the letter you would like removed from your "<<phrasal<<":"<<endl;

cin>>letterToRemove;

s.erase(remove(s.begin(), s.end(),letterToRemove), s.end());

cout<<endl<<s<< "\n"<<endl;
}

and here are initialized variable I used:

int main()

{
char s[1000], phrasal[10], service[50], key1[] = "1", key2[] = "2", key3[] = "3", key4[] = "4", key5[] = "5", key6[] = "6", key0[] = "0", *p, letterToRemove;

int vowel=0, vowel2=0, consonant=0, consonant2=0, b, i, j, k, phrase=0, minusOne, letter, idxToDel;

void pop_back();

char *s_bin;

As you can see, the original 's' is a char array. In the first code sample I have tried converting it into a string array (string str(s)), but that results in the following compiling errors:

  • error: request for member 'erase' in 's', which is of non-class type 'char[1000]'
  • error: request for member 'begin' in 's', which is of non-class type 'char[1000]'
  • error: request for member 'end' in 's', which is of non-class type 'char[1000]'
  • error: request for member 'end' in 's', which is of non-class type 'char[1000]'

The other workaround I've tried was this:

if(strcmp(service, key4)==0)

{std::string s(s);

cout<<endl<<"Please insert the letter you would like removed from your "<<phrasal<<":"<<endl;

cin>>letterToRemove;

s.erase(remove(s.begin(), s.end(),letterToRemove), s.end());

cout<<endl<<s<< "\n"<<endl;
}

Now here's the funny part, I get no errors whatsoever for this one, but the debug crashes as soon as I select the custom letter removal feature. Here's what it says:

"terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create          This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."

Any help would be much appreciated, stuck at this one for a week now!

P.S. Is it okay if I or a moderator deletes this question after it's been answered? I'm quite sure I'm not the first one who asks this, but, as before-hand mentioned, I keep getting these errors, even after following answers from similar questions.

like image 568
Eludium Avatar asked Sep 06 '25 03:09

Eludium


1 Answers

For a char array you have to use std::remove instead of erase, and insert the null-terminator manually:

auto newEnd = std::remove(std::begin(s), std::end(s), letterToRemove);
*newEnd = '\0';
like image 120
emlai Avatar answered Sep 08 '25 01:09

emlai