a class that return an array

I have problem with an array. I created a class where the result is an array of int. but in the main , I can't show the result of my array. Can someone help ?
see code below .

class NombreAsciiLettre
{
public:
NombreAsciiLettre();
int TransformerNombre ( string );
~NombreAsciiLettre(){};

private:
string test, chaine, chaine1, chaine3;
int nb , i,j,k;
};
NombreAsciiLettre::NombreAsciiLettre(){i=0,j=0, k=0;}
int NombreAsciiLettre::TransformerNombre ( string test)
{
char* test1 = new char[test.length()+1];
nb = test.length();
int tableau[nb/2];

while (j<nb/2)
{
chaine = test[i];
chaine1 = test[i+1];
chaine3 = chaine + chaine1;
strcpy(test1,chaine3.c_str());
tableau[j] = atoi(test1);
i = i+2;
j++;
}
*tableau;
}
int main()
{

string test = "6768698688" ;

NombreAsciiLettre L;

L.TransformerNombre(test);
}
I don't see you returning an array anywhere...
the *tableau at the end of my class. The method TransformerNombre ( string test) populates the array tableau and now I want to be able to see it in my main
1) Your function is just returning an int, not an int*
2) You can't return a pointer to local variable, that can easily cause a seg fault. You would need to dynamically allocate the array using new and delete it when you are done (which I notice you didn't do with test1)
OK I am not sure how to do it
so you want me to replace in my method
int tableau[nb/2];
with
int* tableau = new int[nb/2];
but what how do retrieve my array in my main
You would need to return it from the function at the end and then store it in a pointer when you call the function in main.
that's what I have been trying to do for a while. At the end f my function I do return *tableau
and when I called it the main by creating my object
NombreAsciiLettre L;
and then L.TransformerNombre(test); nothing comes out .
I know my function works because I did a cout to test it.
Hi,

I've modified my method to return and int* and created the my array using new but still does not work . I did the cout at the method and in the method and I don't get the same result


class NombreAsciiLettre
{
public:
NombreAsciiLettre();
int* TransformerNombre ( string );
~NombreAsciiLettre(){};

private:
string test, chaine, chaine1, chaine3;
int nb , i,j,k;

};
NombreAsciiLettre::NombreAsciiLettre(){i=0,j=0, k=0;}
int* NombreAsciiLettre::TransformerNombre ( string test)
{
char* test1 = new char[test.length()+1];
nb = test.length();
int* tableau = new int[test.length()/2];

while (j<nb/2)
{
chaine = test[i];
chaine1 = test[i+1];
chaine3 = chaine + chaine1;
strcpy(test1,chaine3.c_str());
tableau[j] = atoi(test1);
i = i+2;
j++;
}
cout << tableau[0] << endl;
cout << tableau[1] << endl;
cout << tableau[2] << endl;
cout << tableau[3] << endl;
cout << tableau[4] << endl;
return tableau;
}

int main()
{

string test = "6768698688" ;
int tableau[test.length()/2];

NombreAsciiLettre L;
L.TransformerNombre(test);

cout << tableau[0] << endl;
cout << tableau[1] << endl;
cout << tableau[2] << endl;
cout << tableau[3] << endl;
cout << tableau[4] << endl;
return 0;
}
Please read this. What you have posted is incomprehensible and is too difficult to read.
http://cplusplus.com/forum/articles/16853/
Last edited on
Thanks for the input kempfighter. I did not how to do it. if you see on line 36 - I want to return an array. - return tableau But When I called the my method in the main line 45-46.
The values are not the same as the one in my method. I do by comparing the values of the cout in my method (30-34) to the result in my main (49-53)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class NombreAsciiLettre
{
public:
       NombreAsciiLettre();
      int* TransformerNombre ( string );
       ~NombreAsciiLettre(){};

private:
       string test, chaine, chaine1, chaine3; 
       int nb , i,j,k;
       
};
NombreAsciiLettre::NombreAsciiLettre(){i=0,j=0, k=0;}
int*  NombreAsciiLettre::TransformerNombre ( string test)
     {
     char* test1 = new char[test.length()+1];
     nb = test.length();
     int* tableau = new int[test.length()/2]; 
        
     while (j<nb/2)
          {
          chaine = test[i];
          chaine1 = test[i+1];        
          chaine3 =  chaine + chaine1;       
          strcpy(test1,chaine3.c_str());  
          tableau[j] = atoi(test1);   
          i = i+2;
          j++;
          }
cout << tableau[0] << endl; 
cout << tableau[1] << endl;
cout << tableau[2] << endl;
cout << tableau[3] << endl;
cout << tableau[4] << endl;    

    return tableau;   
    }

int  main()
{
  
string test = "6768698688" ;
int tableau[test.length()/2];

NombreAsciiLettre L;
L.TransformerNombre(test);


cout << tableau[0] << endl; 
cout << tableau[1] << endl;
cout << tableau[2] << endl;
cout << tableau[3] << endl;
cout << tableau[4] << endl;    
return 0;              
}
Topic archived. No new replies allowed.