Characters

Hello,

I encountered this problem and I was stuck for a days....

my problem is that when i change the
string Nickname; - into a char Nickname[10];

the compiler produces an error message.

*The objective of my program is to trim the characters
i mean i want to remove the #@$&*&*(&) ...etc

so example if the user will input a :

Nickname: J@n3
the compiler would just write the Nickname like this : Jn3


(I mean the purpose of my program is actually about file handling)

here's the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class account_query
{
    private:
        string firstName;
        int total_Balance;
        
    public:
        void read_data();
        void show_data();
        void write_rec();
        void read_rec();
        void search_rec();
        void edit_rec();
        void delete_rec();
};

void account_query::read_data()
{
     char chars[] = "!@#$%^&*()-_+=|\[{]}:;'`<>,?/`~";
     
      cout << "Enter First Name: ";
      cin >> firstName;
     
     for (unsigned int i = 0; i < strlen(chars); ++i)
     {
      firstName.erase (std::remove(firstName.begin(), firstName.end(), chars[i]), firstName.end());
     }
   
      cout << "Enter Balance: ";
      cin >> total_Balance;
      cout << endl;
}


i think the problem is in here :

1
2
3
4
5
6
 
     for (unsigned int i = 0; i < strlen(chars); ++i)
     {
      firstName.erase (std::remove(firstName.begin(), firstName.end(), chars[i]), firstName.end());
      }


thanks... hoping for your help.
Last edited on
closed account (Dy7SLyTq)
when you say you change nick name i assume you mean first name. yes that would be an error because char *'s (which is what char[10] is) arent a class so they dont have an erase method
btw.

how can i remove this

example

Enter nickname : J#n!@

the #n!@ will not be written in the notepad

only the letter J will be written.

i mean removing special characters.
closed account (Dy7SLyTq)
just do out>> firstName[0];
No, he's asking to filter out specific characters. You can do it easily with the C++11 std::copy_if() algorithm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  string name;
  cout << "What's your tag, yo? ";
  getline( cin, name );
  
  name.erase( 
    copy_if( name.begin(), name.end(), name.begin(), (int (*)(int))isalnum ),
    name.end()
    );
  
  cout << "Hello " << name << "!\n";
  
  return 0;
  }

If you are using the older C++ standard, you'll also want the following:

1
2
3
4
5
6
7
8
#include <functional>

template <typename InputIterator, typename OutputIterator, typename UnaryPredicate>
OutputIterator
copy_if( InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate predicate )
  {
  return std::remove_copy_if( first, last, result, std::not1( predicate ) );
  }

Hope this helps.
closed account (Dy7SLyTq)
sorry i didnt see the last line the main part i saw was:

Enter nickname : J#n!@

the #n!@ will not be written in the notepad

only the letter J will be written.

i thought he was looking to filter out special characters but when he wrote
the #n!@ will not be written in the notepad

i thought he just wanted the first character
I know. OP's last post seemed to suggest it.
Topic archived. No new replies allowed.