1

QUESTION

I am trying to update the score after each iteration in my display. How to do so with The following Function.

Code

void DrawString(int x, int y, int width, int height,const string &score,
float*color) {
    float fx = (float)x / width * 2 - 1, fy = (float)y / height * 2 - 1;
    DrawString(fx, fy, score, color);
}
// Function draws a string at given x,y coordinates
void DrawString(float x, float y, const string& score, float * color) {
    glPushMatrix();
    glLoadIdentity();
    glDisable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);

    GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_24;
    if (color)
        glColor3fv(color);
    glRasterPos3f(x, y, 1);
    //  Draw the characters one by one
    for (int i = 0; i < score.size(); i++)
        glutBitmapCharacter(font_style, score[i]);
    glPopMatrix();
}

int main(){
    int score = 45;
    DrawString(250,650,"score : " + score ,colors[RED]);
    //EXAMPLE
    score++;

ERROR

error: invalid operands of types ‘const char [9]’ and ‘float’ to binary ‘operator+’

DrawString(250,650,"score : " + score,colors[RED]);

Community
  • 1
  • 1
  • `DrawString(250,650,"score : " + score ,colors[RED]);` -- What are you trying to accomplish with this line of code? What is that third parameter supposed to denote? – PaulMcKenzie Dec 01 '19 at 08:00
  • Score : 45 Score : 46 Score : 47 And so on – TechJuice02 Dec 01 '19 at 08:02
  • Well C++ is not Java or whatever other language you were using. You cannot add an `int` to a string literal and get a string. You could have seen this by doing: `std::string test = "score : " + score;` and see the same or similar error. Now that you see the issue, there are many duplicates here that answer the question of "adding" a string to an int. – PaulMcKenzie Dec 01 '19 at 08:04
  • Can you help me sorting out this error.. – TechJuice02 Dec 01 '19 at 08:09
  • [How to convert int to string](/questions/5590381/easiest-way-to-convert-int-to-string-in-c). Both sides of the `+` should be `std::string`. – PaulMcKenzie Dec 01 '19 at 08:13

0 Answers0