C++ removing chars from a std::string

What's wrong with my 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    std::vector <unsigned char> _colors (QD * 20);

    struct RGB
    {
        RGB() : R(0), G(0), B(0) { }
        unsigned char R, G, B;
    };

    bool RGB_Exist(char R, char G, char B, char delimiter)
    {
        return (R == 'R' && G == 'G' && B == 'B' && delimiter == ':');
    }

    void Formatter(std::string& RGBformat)
    {
        RGB rgb;
        RGBformat.erase(0, 1); //remove the indicator '\r'

        for (int i = 0;i < RGBformat.length(); ++i)
        {
            if (RGBformat[i] == '<')
            {
                if (RGB_Exist(RGBformat[i + 1], RGBformat[i + 2], RGBformat[i + 3], RGBformat[i + 4]))
                {
                    RGB_Read(RGBformat, i, rgb);
                    i = 0;
                }
            }
            _colors.push_back(rgb.R); _colors.push_back(rgb.G); _colors.push_back(rgb.B); 
            _colors.push_back(rgb.R); _colors.push_back(rgb.G); _colors.push_back(rgb.B); 
            _colors.push_back(rgb.R); _colors.push_back(rgb.G); _colors.push_back(rgb.B); 
            _colors.push_back(rgb.R); _colors.push_back(rgb.G); _colors.push_back(rgb.B);
        }

    }

    void RGB_Read(std::string& RGBformat, int index, RGB& rgb)
    {
        int i = index + 5; //we are now next to character ':'
        std::string value;
        int toNumber;

        while (RGBformat[i] != ',')
        {
            value += RGBformat[i++];
        }
        ++i;
        std::stringstream(value) >> toNumber;
        rgb.R = toNumber;
        value = "";

        while (RGBformat[i] != ',')
        {
            value += RGBformat[i++];
        }
        ++i;
        std::stringstream(value) >> toNumber;
        value = "";
        rgb.G = toNumber;

        while (RGBformat[i] != '>')
        {
            value += RGBformat[i++];
        }
        ++i;
        std::stringstream(value) >> toNumber;
        value = "";
        rgb.B = toNumber;

        //I got the right result here which is
        //start: <, end: >
        printf("start: %c, end: %c\n", RGBformat[index], RGBformat[i]);
        //but fail in this one
        //this one should erase from '<' until it finds '>'
        RGBformat.erase(index, i);
    
    }


Then the string that will be formatted is:

"\r<RGB:255, 255, 0>ABCD<RGB:0, 0, 255>EFGH<RGB:0,255,0> bla bla bla";

where '\r' is the indicator that the string passed will be formatted by color

After running this code, the first <RGB> expression read well but on the following <RGB> format, the string erased not as expected. I got the result of:

"ABCD bla bla";

which is should be

"ABCDEFGH bla bla bla";

I want to erase only the <RGB:?,?,?> expression. Whats wrong in my code?
And also I need to pass first the std::stringstream(value) to an `int` before I put it into `unsigned char` because I get different value when passing it directly. How does std::stringstream(value) behave exactly? Or can you suggest much better string to int` converter?

I would highly suggest using a debugger to look at your code step by step.

Note that erase(index, i) does not erase all characters from index to i, it erases i characters starting from index; so you probably want erase(index, i-index) or something instead.
Topic archived. No new replies allowed.