Pregunta

I tried to assign different variables value to double array in loop but the whole array has just 1 value

double *float_array;
float_array = new double [dynamic_variable]; // debugger does tell me its size     
stringstream ss1(line);

string s1;
string s2 = "2.1";
double test= atof(s2.c_str());
while (getline(ss1,s1,','))
{
    float_array[count] = atof(s1.c_str());
    count++;
}
count = 0;
root->data = float_array;
root->next = new node;

showing some more code

class node
{

public:
     double * data;
    node *next;
    node(void);
    ~node(void);
};

int _tmain(int argc, _TCHAR* argv[])
{
    double arr[4]= {0.689997};
    double * float_array;
    string file_name; 
    string line,token;
    string path= "D:\\DM\\Assignment No. 1\\";
    cin>>file_name;
     file_name= path + file_name;
     ifstream aa;
     aa.open(file_name,ios::in|ios::out);
     node *root; int float_arr_size=0;int count=0;
     //aa.open(file_name,ios::in|ios::out);
     if(aa.is_open())
     {

        while(!aa.eof())
        {
            aa>>line;
            cout<<line<<endl;
            cout<<endl;
            stringstream ss(line);
            string s;

            while (getline(ss, s, ','))
            {
            float_arr_size++;
            }

            float_array= new double[float_arr_size];
            s="";

            stringstream ss1(line);
            string s1;
            string s2= "2.1";
            double test= atof(s2.c_str());
            while(getline(ss1,s1,','))
            {
                float_array[count] =  atof(s1.c_str());
                count++;
            }
            count = 0;
            root->data =float_array;
            root->next= new node;
        }
     }
    aa.close();

    return 0;
}

I get perfect conversion in my test variable. I also get "2.1" in s1, but in debugger I get only one value in float_array that is 5.0999945. No further indexes have any further values. After having test variable value converted correctly, I see problem in my dynamic array.

Is there a solution, please?

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top