For statement - string

I have a variable that is a string called tone_id, and I am trying to make a for statement when a value of ‘joy’ then draw this circle (the values come from a json file). Can anybody tell me what I’m doing wrong?

string tone_id;

//-----

std::string tone_id = result["sentences_tone"][sentence_id]["tones"][sentence_id]["tone_id"].asString();

void ofApp::draw(){

for (string tone_id = “joy” ; tone_id <100; tone_id++) {
ofSetColor(29, 100, 90);
ofDrawCircle(300, 600, 90);
}

I am trying to get or “collect” all the data from this json file that has the value of “joy”:

{
“document_tone”: {
“tones”: [
{
“score”: 0.515079,
“tone_id”: “joy”,
“tone_name”: “Joy”
}

Hi! In that for loop, you initialize the variable as a string but then use it as an integer by comparing it to 100 and increasing its value with ++. That doesn’t work. You can’t do such operations on a string.

Re-reading your question, I believe you want an animated visualization of the entries in the json where tone_id == joy. Is that right? Then you would need an integer variable that increases on every animation frame. That integer value would serve as an index of which element to read from the json array, so on every frame you look at the next value. You would not use a for loop, unless you want to visualize all the values at once, without animation. Did I get it right?

Yes exactly! How do i then do a for loop or any operation that would fetch the ‘joy’ value from the tone_id, for a string? I tried defining it as an int but that did not work as I expected since the value ‘joy’ has characters.

You are correct, I want to visualize this data, my best hope would be in animation but given that I am such a beginner I will settle for a non-animation and just circles. The tones have different score values as well, and I would like the size of the circles to depend on this - if that makes sense? So let’s say the tone_id is ‘joy’ and the score value is 0.8 - then I want my circle to be yellow and a large size. If it would be, let’s say ‘fear’ and score is 0.3 I would want the color to be purple and size to be smaller. How would I do this?

I hope I am being clear and thank you so much for your reply.

I have been experimenting with the visualization, this is the code as of now, but it very tedious since I don’t have any operations that I can use, right now I am just making one and one circle since i don’t know make operations with the output of the json:

void ofApp::setup(){

ofSetFrameRate(40);

}

void ofApp::draw(){

float radius = 250;
for (int i = 0; i < 1; i++) {

float size = ofRandom(20.f); // I don't want this to be ofRandom, I want to make some kind of a for/if statement, so the size would depend on the int score value

float deg = ofRandom(360.f) + ofGetFrameNum() * ofMap(size, 0, 20, 0.8, 0.15);
float noise_value = ofMap(ofNoise(i * 0.5, ofGetFrameNum() * 0.005), 0, 1, 0.8, 1.2);
float x = radius * noise_value * cos(deg * DEG_TO_RAD);
float y = radius * noise_value * sin(deg * DEG_TO_RAD);
ofPoint point = ofPoint(x, y);

ofColor joycolor;
joycolor.setHsb(290, 160, 619);

for (int score = 0.515079; score <1; score++) {
ofSetColor(joycolor, 139);
ofDrawCircle(point, size);
}

for (int score = 0.537984; score <1; score++) {
    ofSetColor(joycolor, 139);
    ofDrawCircle(point*1.2, 21);
}

for (int score = 0.940704; score <1; score++) {
    ofSetColor(joycolor, 139);
    ofDrawCircle(point*0.8, 30);

}

}

HOW I WANT IT TO LOOK LIKE (APPROXIMATELY):

  1. The code you are posting is not very readable. You can fix it by editing your posts above, and use “code fencing”. What that means is: before the first line of code (above setup for instance) insert a line with these 6 characters: ```cpp
    To finish the block of code, insert these 3 characters: ```
    It will become more readable :tada:
  2. By looking at the for loops I believe you may be trying to do something advanced before understanding the basics. You are trying to store floating point numbers like 0.537984 into an int variable, so the fractional part would be discarded becoming just 0. Then, if you count from 0 to 1 in steps of 1 (as your program does), you just get one step, so the for loop doesn’t do much, except running the first case (zero).
  3. Do you know http://learncpp.com/ and the OF Book http://openframeworks.cc/ofBook/chapters/foreword.html sites? They can be helpful :slight_smile:
  4. OpenFrameworks comes with a jsonExample in the examples/input_output/ folder. That example loads a json with an array of arrays. The outer array contains arrays of vertices. Do you understand how that one works?

Based on the jsonExample and your data:

test.json

{
  "document_tone": {
    "tones": [
      {
        "score": 0.2,
        "tone_id": "joy",
        "tone_name": "Joy"
      },
      {
        "score": 0.4,
        "tone_id": "blu",
        "tone_name": "Bli"
      },
      {
        "score": 0.6,
        "tone_id": "joy",
        "tone_name": "Joy"
      }
    ]
  }
}

Inside ofApp::setup()

    ofFile file("test.json");
    if(file.exists()) {
        file >> js;
        for(auto & tone : js["document_tone"]["tones"]) {
            if(tone["tone_id"] == "joy") {
                float score = static_cast<float>(tone["score"]);
                if(score > 0.5f) {
                    ofLog() << "big joy";
                } else {
                    ofLog() << "small joy";
                }
            } else {
                ofLog() << "no joy";
            }
        }
    }

Result:

[notice ] small joy
[notice ] no joy
[notice ] big joy
1 Like

Thank you for very helpful tips. This has helped a lot!