PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Re: integer to char*
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: integer to char*

Réponse
 
LinkBack Outils de la discussion
Vieux 15/01/2008, 19h35   #1
Tristan Wibberley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integer to char*


On Tue, 2008-01-15 at 11:15 -0800, rob.ahlberg@gmail.com wrote:
> I got an integer what I trying to use with allegro function
> textout_ex() but it wants an char[]/char* as arg... And I really don't
> know how to cast it to one...


You don't cast your int, you convert it.

#include <sstream>
#include <ostream>

....

std:stringstream s;
s << my_int;
textout_ex(..., s.str().c_str(), ...);

or, if the allegro textout_ex function barfs due to something about
"const", try:

#include <vector>
#include <sstream>
#include <ostream>

....

std:stringstream s;
s << my_int;
std::vector<char> v(s.str().begin(), s.str().end());
v.push_back('\0');
textout_ex(..., &v[0], ...); // &v[0] is okay because v.size() >= 1

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.

  Réponse avec citation
Vieux 15/01/2008, 23h02   #2
rob.ahlberg@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integer to char*

On 15 Jan, 20:35, Tristan Wibberley <maihem-...@maihem.org> wrote:
> On Tue, 2008-01-15 at 11:15 -0800, rob.ahlb...@gmail.com wrote:
> > I got an integer what I trying to use with allegro function
> > textout_ex() but it wants an char[]/char* as arg... And I really don't
> > know how to cast it to one...

>
> You don't cast your int, you convert it.
>
> #include <sstream>
> #include <ostream>
>
> ...
>
> std:stringstream s;
> s << my_int;
> textout_ex(..., s.str().c_str(), ...);
>
> or, if the allegro textout_ex function barfs due to something about
> "const", try:
>
> #include <vector>
> #include <sstream>
> #include <ostream>
>
> ...
>
> std:stringstream s;
> s << my_int;
> std::vector<char> v(s.str().begin(), s.str().end());
> v.push_back('\0');
> textout_ex(..., &v[0], ...); // &v[0] is okay because v.size() >= 1
>
> --
> Tristan Wibberley
>
> Any opinion expressed is mine (or else I'm playing devils advocate for
> the sake of a good argument). My employer had nothing to do with this
> communication.


Yeah the programming guru at my school ed me with the string
stream. Unfortunately I cannot clear the string.. It's a little game
I'm currently at.. And I got a function to determine the fps.Then I
got 'int fps' And I do like:
ss << fps;
print(ss.str().c_str());
ss.flush();
But that's the problem... It wont clear the string. It just add the
new fps after the other one and so on. I need to clear it...
  Réponse avec citation
Vieux 15/01/2008, 23h05   #3
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integer to char*

rob.ahlberg@gmail.com wrote:
> [..]
> Yeah the programming guru at my school ed me with the string
> stream. Unfortunately I cannot clear the string.. It's a little game
> I'm currently at.. And I got a function to determine the fps.Then I
> got 'int fps' And I do like:
> ss << fps;
> print(ss.str().c_str());
> ss.flush();
> But that's the problem... It wont clear the string. It just add the
> new fps after the other one and so on. I need to clear it...


This should clear the stream, IIRC:

ss.str().clear();

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 16/01/2008, 09h30   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integer to char*

On Jan 16, 12:05 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> rob.ahlb...@gmail.com wrote:
> > [..]
> > Yeah the programming guru at my school ed me with the string
> > stream. Unfortunately I cannot clear the string.. It's a little game
> > I'm currently at.. And I got a function to determine the fps.Then I
> > got 'int fps' And I do like:
> > ss << fps;
> > print(ss.str().c_str());
> > ss.flush();
> > But that's the problem... It wont clear the string. It just add the
> > new fps after the other one and so on. I need to clear it...


> This should clear the stream, IIRC:


> ss.str().clear();


I don't see how. stringstream::str() returns by value; calling
a member function on the temporary it returns can't have any
effect on the stream itself. There is a non const version of
str():
ss.str( std::string() ) ;
which would do the job, but frankly, the real solution is much
simpler: if you want a new ostringstream, use a new
ostringstream. (This is the sort of code which would be in a
function anyway, with the ostringstream as a local variable. So
each time you call the function, you get a new instance.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 16/01/2008, 14h53   #5
rob.ahlberg@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integer to char*

On 16 Jan, 00:05, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> rob.ahlb...@gmail.com wrote:
> > [..]
> > Yeah the programming guru at my school ed me with the string
> > stream. Unfortunately I cannot clear the string.. It's a little game
> > I'm currently at.. And I got a function to determine the fps.Then I
> > got 'int fps' And I do like:
> > ss << fps;
> > print(ss.str().c_str());
> > ss.flush();
> > But that's the problem... It wont clear the string. It just add the
> > new fps after the other one and so on. I need to clear it...

>
> This should clear the stream, IIRC:
>
> ss.str().clear();
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Aha.. I just wroted ss.clear();
Thanks for the
  Réponse avec citation
Vieux 16/01/2008, 15h36   #6
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integer to char*

rob.ahlberg@gmail.com wrote:
> On 16 Jan, 00:05, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> rob.ahlb...@gmail.com wrote:
>>> [..]
>>> Yeah the programming guru at my school ed me with the string
>>> stream. Unfortunately I cannot clear the string.. It's a little game
>>> I'm currently at.. And I got a function to determine the fps.Then I
>>> got 'int fps' And I do like:
>>> ss << fps;
>>> print(ss.str().c_str());
>>> ss.flush();
>>> But that's the problem... It wont clear the string. It just add the
>>> new fps after the other one and so on. I need to clear it...

>>
>> This should clear the stream, IIRC:
>>
>> ss.str().clear();
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> Aha.. I just wroted ss.clear();
> Thanks for the


You probably need to do 'ss.clear()' as well, in case there are any
error bits set...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18093 seconds with 14 queries