Search code examples
c++openglglut

glutStrokeCharacter to draw an int variable


I'm trying to draw an int to screen using the method glutStrokeCharacter using the following code

    int fuel = 400;
    std::string t = std::to_string(fuel);
    char *fueltxt;
    for (fueltxt = t; *fueltxt != '\0'; fueltxt++)
        {
            glutStrokeCharacter(GLUT_STROKE_ROMAN , *fueltxt);
        }

I'm working on XCode and I'm getting the following error at the for loop, specifically at fuel txt = t

"Assigning to 'char*' from incompatible type 'std::string'(aka 'basic_string, allocator >')


Solution

  • Do it the C++ way, using iterators:

    int fuel = 400;
    std::string t = std::to_string(fuel);
    
    for (auto c = t.begin(); c != t.end(); ++c)
    {
      glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
    }
    

    If your compiler supports them, you could even use a range-based for loop:

    int fuel = 400;
    
    for (char c : std::to_string(fuel))
    {
      glutStrokeCharacter(GLUT_STROKE_ROMAN, c);
    }