Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ converting an int to an array of chars? [duplicate]

Possible Duplicate:
C++ convert int and string to char*

Hello, i am making a game and I have a score board in it. The score is stored in an int variable but the library im using for the game needs an array of chars for its text outputting for my scoreboard.

So how do i turn an int into an array of chars?

int score = 1234;  // this stores the current score

dbText( 100,100, need_the_score_here_but_has_to_be_a_char_array); 
// this function takes in X, Y cords and the text to output via a char array

The library im using is DarkGDK.

tyvm :)

like image 491
EddieV223 Avatar asked Dec 05 '25 15:12

EddieV223


2 Answers

ostringstream sout;
sout << score;
dbText(100,100, sout.str().c_str());
like image 121
Yakov Galka Avatar answered Dec 07 '25 05:12

Yakov Galka


Use sprintf

#include <stdio.h>

int main () {
  int score = 1234; // this stores the current score
  char buffer [50];
  sprintf (buffer, "%d", score);
  dbText( 100,100,buffer);

}
like image 33
Preet Sangha Avatar answered Dec 07 '25 05:12

Preet Sangha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!