2

I want an int value to display in screen as a string. This is for a game I am doing in opengl. I have something like this:

char *string = "0"; // to declare

sprintf (label,"%d" ,string); // This prints 0

This works perfect to print the 0 in the screen, however as you might understand I want the 0 to be changing. I tried converting int to string and trying to assign this to the char *string, but i think it is not possible. I am a newbie in C++ so I do not know much about I would much appreciate your help with this. What I want to achieve is this:

char *string = "0"; // to declare
int number = 90; // declare int to be converted;

sprintf (label,"%d" ,string); // This prints 90

I have found converting methods for int to char methods, howevernon have solved my issue. Thank you for all your help in advance.

Vico Pelaez
  • 51
  • 2
  • 4
  • 7
  • What about `printf` with `%d`? – Jon Nov 03 '12 at 14:57
  • what is the role of `int number` in all of this? – juanchopanza Nov 03 '12 at 14:57
  • There are many ways of converting [string to integers and vice versa][1]. [1]: http://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c – nogard Nov 03 '12 at 14:58
  • basically the number will be the score, and this will be changing constantly so I want to refresh the score on screen – Vico Pelaez Nov 03 '12 at 15:00
  • 1
    @Scooter: Ask the author before making such a fundamental change to the question. There is nothing to indicate that the question is C only. – Martin York Nov 03 '12 at 15:10
  • @LokiAstari Well there was this: "Thanks but I am not using just c++ this is a game for opengl and to be able to print this in opengl i need to use it in that format. Thank you!" In rereading I see the _just_ which I missed because the comment came in response to a iostream C++ solution, which I thought meant he was looking for a C solution. – Scooter Nov 03 '12 at 15:24
  • This answer may help you. > http://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c – dengjiebin Jan 21 '13 at 10:14

3 Answers3

5

If all you want to do is print a number to the screen, then you can stream to std::cout:

#include <iostream>

int nubmer = ....;

std::cout << number;

Otherwise, you can stream the number into an std::ostringstream, and get the underlying const char*:

std::strimgstream o;
o << number;
const char* string_ = o.str().c_str();
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks but I am not using just c++ this is a game for opengl and to be able to print this in opengl i need to use it in that format. Thank you! – Vico Pelaez Nov 03 '12 at 15:02
  • @juanchopanza The question was originally tagged C++ when you answered it. I retagged it as C when he said he was using C. The downvoter probably saw your C++ answer and didn't realize that you made it before it was retagged. – Scooter Nov 03 '12 at 15:10
  • @Scooter OP says "I am not using just C++", which implies C++ is OK, as long as one gets a `char*` out of it somehow. – juanchopanza Nov 03 '12 at 15:12
  • 1
    Yep, I've undone my two downvotes now that this has been explained. – Barmar Nov 03 '12 at 15:12
4

Use this:

std::stringstream val;

val << number;

val.str();         // Gets you a C++ std::string
val.str().c_str(); // Gets you a C-String
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • @Barmar the question was tagged C++, and OP states in a comment "I am not using just C++", which suggests C++ is OK, but in coordination with a C some API. – juanchopanza Nov 03 '12 at 15:11
1
   char label[100] = {"0"};
   printf("%s\n",label);
   int number = 90;
   sprintf(label,"%d",number);
   printf("%s\n",label);
   sprintf(label,"%d",number + 1);
   printf("%s\n",label);

output:

0
90
91
Scooter
  • 6,802
  • 8
  • 41
  • 64