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()
{
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
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.
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
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)