george rizk
george rizk

Reputation: 99

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 >')

Upvotes: 1

Views: 518

Answers (1)

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);
}

Upvotes: 3

Related Questions