Need help with a C++ string sanitizer

Need help with a C++ string sanitizer

Post by Desoxena on Fri Jan 07, 2011 6:42 pm
([msg=51851]see Need help with a C++ string sanitizer[/msg])

I am trying to make a program that you enter in a string, and it will sanitize the string. (example: you enter "h#e$$l*lo, and it will output "hello")
here is my current source code:
Code: Select all
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

using namespace std;

int main()
{
    int i, j;
    string dstr;
    string illegalChar[] = {"#", "@", "!", "$", "%", "?"};
    cout << "enter a string" << endl;
    cin >> dstr;
    for (i=0;i<dstr.length();i++) {
        for (j=0; j<5; j++) {
            if(dstr.at(i)==illegalChar[j])//in compiler, this line was highlighted for error given below
            {
                          dstr.replace(i, dstr.length(), "");
                          }
                          }
                          }
                          cout << dstr << endl;
                          system("pause");
                          }



my problem is that i get this error message: 18 C:\Users\Evan Rabdau\successful C++ projects\TASKMASTER360\stringsanitizer.cpp no match for 'operator==' in '(&dstr)->std::basic_string<_CharT, _Traits, _Alloc>::at [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((unsigned int)i)) == illegalChar[j]'
Desoxena
New User
New User
 
Posts: 25
Joined: Sun Oct 03, 2010 7:13 pm
Blog: View Blog (0)


Re: Need help with a C++ string sanitizer

Post by Assassian360 on Fri Jan 07, 2011 7:44 pm
([msg=51854]see Re: Need help with a C++ string sanitizer[/msg])

if(dstr.at(i)==illegalChar[j])

This error on this line would because the "at" function doesn't return a string. It returns a char type.
http://www.cplusplus.com/reference/string/string/at/

So if you change: string illegalChar[] = {"#", "@", "!", "$", "%", "?"};
To have type char, and use single quotes around each element, it should work.
I haven't bothered testing it though, so let us know if that works or doesn't.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests