making a interger a character

I have two variables a and b.

need to make the values holded by them char*. How can i do that?

I need to print the number down. It is a short number. I need to print it to GUI, so need to make it char*

please help. its urgent
Last edited on
You can't change the type of a variable, but you can either cast the integers to a char or assign them to a char.

Casting:
1
2
int a = 97;
std::cout << static_cast<char>(a) << std::endl;    // outputs 'a' 


Reassigning:
1
2
3
int var = 97;
char a = var;
std::cout << a << std::endl;    // outputs 'a' 


void glutPrint(float x, float y, LPVOID font, char* text, float red, float green, float blue)

I need to enter char* text. How can i do that?
char* are c strings. Character arrays. You could do something like:

1
2
3
4
5
6
7
8
9
#include <sstream>

//in code
int someInt = 3463;
stringstream conv;

conv << someInt;

glutPrint(X, Y, FONT, conv.str().c_str(), REd, GREEN, BLUE);
str undeclared what can i do?
to be more specific what I need to do is:

if int a = 123
want to make
1
2
3
4
char d[3];
d[0] = 1;
d[1] = 2;
d[2] = 3;
ModShop nailed it:

1
2
3
4
5
6
int someInt = 3463;
stringstream conv;

conv << someInt;

glutPrint(X, Y, FONT, conv.str().c_str(), REd, GREEN, BLUE);


Just make sure you #include <sstream> and are using namespace std.
I got:
1
2
3
4
5
6
7
8
void loadstats()
{
    stringstream conv;

    conv << stats.win;
    glutPrint(-1.5, 3,GLUT_BITMAP_TIMES_ROMAN_24, conv.str().c_str(), 1.0, 1.0, 1.0);

}


It gives me the error:

error: invalid conversion from `const char*' to `char*'|
error:   initializing argument 4 of `void glutPrint(float, float, void*, char*, float, float, float)'|
||=== Build finished: 2 errors, 7 warnings ===|

I do have both of them included
Ah, yuck. glut isn't const correct. Shame on them. Shame shame shame.

you'll have to const cast it.

 
glutPrint(-1.5, 3,GLUT_BITMAP_TIMES_ROMAN_24, const_cast<char*>(conv.str().c_str()), 1.0, 1.0, 1.0);
Last edited on
I made the function so shame on me.
not really a shame bacause I am a beginner. lol

anyway.. thanks sooo much.
oh. In that case, don't do const_cast, but change the function to take a const char* instead of a char*.
Topic archived. No new replies allowed.