Conversion int en char*

arwenita Messages postés 5 Date d'inscription dimanche 26 octobre 2003 Statut Membre Dernière intervention 27 novembre 2004 - 11 juin 2004 à 21:47
cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 - 12 juin 2004 à 11:18
Encore un petit problème en C++...
Dans mon programme, je voudrais afficher une liste de nombres (premiers), que je récupère de la fonction Crible(n).
Le seul problème, ce que je n'arrive a afficher que des barres à la place de nombres. Il faudrait que je convertise les entiers i en char*, ca que je ne sais pas faire.

bool* Liste = new bool[n+1];
   char* Affich;
   int j=0;
   Liste = Crible(n);
   for(int i=0; i<n+1;i++)
   {
        if(Liste[i]==true)
        {
           Affich[j]=i;
           j++;
           Affich[j]=' ';
           j++;
        }
   }
   MemoGene->Lines->Add(Affich);
   delete Liste;


Si quelqu'un sait faire cette conversion ou s'il a une autre idée, il serait le bienvenu !
Merci d'avance !

3 réponses

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
11 juin 2004 à 21:52
Comme d'hab, *Affich POINTE SUR QUOI ???

ciao...
BruNews, Admin CS, MVP Visual C++
0
ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
11 juin 2004 à 23:13
#include <stdlib.h>
char *itoa(int value, char *string, int radix);

Description

Converts an integer to a string.
itoa converts value to a null-terminated string and stores the result in string. With itoa, value is an integer.
radix specifies the base to be used in converting value; it must be between 2 and 36, inclusive. If value is negative and radix is 10, the first character of string is the minus sign (-).

Note: The space allocated for string must be large enough to hold the returned string, including the terminating null character (\0). itoa can return up to 17 bytes.

Return Value

itoa returns a pointer to string.
0
cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 7
12 juin 2004 à 11:18
ou en c++, avec char * si tu veux...

#include
#include <sstream>
#include <cstring>

inline const char *cpp_itoa(int n)
{
std::ostringstream oss;
oss << n;
if( !oss ) return 0;
return oss.str().c_str();
}

int main()
{
char tmp[200];

std::strcpy(tmp, cpp_itoa(25) );

std::cout << tmp << '\n';
}
0
Rejoignez-nous