Help #include<algorithm>

Hello I have an error when using <algorithm> any help?
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 #include<iostream>
#include<algorithm>
#include<windows.h>
#include<fstream>
#include<string>


using namespace std;

int main()
{
    string text, filename;
    cin>>filename;
    ifstream myfile(filename.c_str());

    myfile >> text;
    replace(text.begin(), text.end(), ",", "a");
    replace(text.begin(), text.end(), "<", "b");
    replace(text.begin(), text.end(), ">", "c");
    replace(text.begin(), text.end(), "?", "d");
    replace(text.begin(), text.end(), "/", "e");
    replace(text.begin(), text.end(), "|", "f");
    replace(text.begin(), text.end(), "'", "g");
    replace(text.begin(), text.end(), ":", "h");
    replace(text.begin(), text.end(), ";", "i");
    replace(text.begin(), text.end(), "}", "j");
    replace(text.begin(), text.end(), "{", "k");
    replace(text.begin(), text.end(), "]", "l");
    replace(text.begin(), text.end(), "[", "m");
    replace(text.begin(), text.end(), "+", "n");
    replace(text.begin(), text.end(), "_", "o");
    replace(text.begin(), text.end(), ")", "p");
    replace(text.begin(), text.end(), "(", "q");
    replace(text.begin(), text.end(), "*", "r");
    replace(text.begin(), text.end(), "&", "s");
    replace(text.begin(), text.end(), "^", "t");
    replace(text.begin(), text.end(), "%", "u");
    replace(text.begin(), text.end(), "$", "v");
    replace(text.begin(), text.end(), "#", "w");
    replace(text.begin(), text.end(), "@", "x");
    replace(text.begin(), text.end(), "!", "y");
    replace(text.begin(), text.end(), "~", "z");
    replace(text.begin(), text.end(), "`", " ");
    cout<<text;

    system("pause");
    return 0;
}
If you want replace characters, pass characters to function, not string literals.
'x' != "x"
Any example?
Thanks!
1
2
3
std::string x = "Hello"; //Works, initializing string with string literal
x[2] = "m"; //Does not work: cannot assign string literal to char
x[2] = 'm'; //Works assigning character literal to char. 
Oh my god stupid me, sorry, I am such a idiot :D
Thanks!!!
This is a good example of where data can be preferable to 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
33
34
35
36
37
    const char *translation =
        ",a"
        "<b"
        ">c"
        "?d"
        "/e"
        "|f"
        "'g"
        ":h"
        ";i"
        "}j"
        "{k"
        "]l"
        "[m"
        "+n"
        "_o"
        ")p"
        "(q"
        "*r"
        "&s"
        "^t"
        "%u"
        "$v"
        "#w"
        "@x"
        "!y"
        "~z"
        "` ";

    cin>>filename;
    ifstream myfile(filename.c_str());
    myfile >> text;

    for (const char *cp = translation; *cp; cp += 2) {
        replace(text.begin(), text.end(), cp[0], cp[1]);
    }
    cout<<text;

Each pair of characters in the translation string is a from/to pair.
To show the pairs, I've taken advantage of the fact that the string literal "hello" " world" is the same as "hello world".

Some may cringe at the use of a C string here. It's easy enough to use an std::string instead if you prefer.

Note that your code will read only the first word of the file. If you intend to read and translate the entire file then you'll need to do something different.

If the code translates the entire file then, in the real world, speed would matter. In that case, it would be better to build a lookup table that maps all 256 characters. Most entries would map to themselves but the characters in your "from" list would map to your "to" list. Once you had this table, you could simply map every character to its equivalent. The only hard part about this is that you have to ensure it will work with signed characters. This won't work:
1
2
3
4
char ch;
while (cin >> ch) {
    cout << table[ch];
}

because if characters are signed then their numeric values are -128-127, not 0-255. a naive fix would change line 3 to
cout << table[static_cast<unsigned>(ch)];
but that won't work either because it will widen the signed character to the width of an unsigned first, and then cast it to unsigned. You have to cast to unsigned char first:
cout << table[static_cast<unsigned char>(ch)];


Thanks!!!
Topic archived. No new replies allowed.