(using C++, OpenGL/GLUT, Xcode, OSX)
I’m looking for help in drawing random characters to the screen.
I’ve separated the task into 2 parts…
(1) I am able to pick a random UPPER_CASE letter using the following:
int launchCode1_random = rand() % 26;
char c1 = (char)(launchCode1_random+65);
cout << c1 << "\n";
(2) And I can draw strings in OpenGL using the following:
string launchCode1 = "thing";
void * fontlaunchCode1 = GLUT_BITMAP_9_BY_15;
for (string::iterator i = launchCode1.begin(); i != launchCode1.end(); ++i)
{
char c = *i;
glutBitmapCharacter(fontlaunchCode1, c);
}
… What I would like to write in step 2 above, however, is:
string launchCode1 = c1;
In doing so, I receive the following error: “No viable conversion from ‘char’ to ‘string’. That makes sense to me on the surface. But I don’t fully understand why or how. And if there is an alternative workaround that I’m not seeing, I would love to hear your thoughts on the subject.
edit…..
Here’s the final, working code with Pubby’s help:
int launchCode1_random = rand() % 26;
char c1 = (char)(launchCode1_random+65);
cout << c1 << "\n";
string launchCode1 (1, c1);
void * fontlaunchCode1 = GLUT_BITMAP_9_BY_15;
for (string::iterator i = launchCode1.begin(); i != launchCode1.end(); ++i)
{
char c = *i;
glutBitmapCharacter(fontlaunchCode1, c);
}
If you want to construct a
std::stringfrom a character you can do this:Using this constructor:
http://en.cppreference.com/w/cpp/string/basic_string/basic_string
I don’t fully understand what you’re doing though. Why even bother with
std::stringif all you want is a single character? Just do this: