Draw matrix/block out of chars from some strings …

Dear community,

for my new university educational/art project, I need to draw strings of text in the form of a matrix.

I’m reading values from a text file, which are split in lines and then in values. One line e.g. containes the values:

x=000
y=000
r=026
g=025
b=030

I need to draw/cout them first in a block like:

- x 0 0 0
y r 0 2 6
0 g 0 2 5
0 b 0 3 0
0 . . , ;

Where the characters -, ., ., ,, ; are fixed at their position, also x, y, r, g, b. Plus I think I need the spaces in between the characters in the horizontal lines. In other words, the numeral values change in each line, so have to change in each block.

The code I’m using so far looks like this:

void ofApp::setup() {
    auto lines = ofSplitString(ofBufferFromFile("of.txt").getText(), "\n");

    for (auto & line : lines) {
        auto values = ofSplitString(line, "\t");

        for (auto & value : values) {
            cout << value << endl;
        }
    }
}

Could anyone please help me getting this done? Thank you so very much.

Kindest regards!

PS: This is how far I came. But the code looks not really nice, I bet you can automate this. Plus, it throws out of range errors …

error:

[…]
- x 0 8 4
y r 0 4 9
0 g 0 4 2
9 b 0 3 6
9 . . , ;
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector

code:

#include "ofApp.h"

void ofApp::setup() {
    auto lines = ofSplitString(ofBufferFromFile("of.txt").getText(), "\n");

    for (auto & line : lines) {
        auto values = ofSplitString(line, "\t");

        auto & x1 = values.at(0)[2];
        auto & x2 = values.at(0)[3];
        auto & x3 = values.at(0)[4];

        auto & y1 = values.at(1)[2];
        auto & y2 = values.at(1)[3];
        auto & y3 = values.at(1)[4];

        auto & r1 = values.at(2)[2];
        auto & r2 = values.at(2)[3];
        auto & r3 = values.at(2)[4];

        auto & g1 = values.at(3)[2];
        auto & g2 = values.at(3)[3];
        auto & g3 = values.at(3)[4];

        auto & b1 = values.at(4)[2];
        auto & b2 = values.at(4)[3];
        auto & b3 = values.at(4)[4];

        cout << "-" << " "  << "x"  << " "  << x1   << " "  << x2   << " "  << x3   << endl;
        cout << "y" << " "  << "r"  << " "  << r1   << " "  << r2   << " "  << r3   << endl;
        cout << y1  << " "  << "g"  << " "  << g1   << " "  << g2   << " "  << g3   << endl;
        cout << y2  << " "  << "b"  << " "  << b1   << " "  << b2   << " "  << b3   << endl;
        cout << y3  << " "  << "."  << " "  << "."  << " "  << ","  << " "  << ";"  << endl;
        cout << "\n" << endl;
    }
}

It seems lines has a size of 8501 instead of 8500 … but I don’t know how to fix this.

I could do lines.resize(8500);, but that doesn’t look like good quality code …

EDIT: lines.resize(lines.size() - 1); is at least somehow better.

have you looked in to str.at(i)
http://www.cplusplus.com/reference/string/string/at/

maybe it will help