我试图在循环中为双数组分配不同的变量值,但整个数组只有 1 个值
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;
显示更多代码
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;
}
我在我的测试变量中得到了完美的转换。我也得到“2.1” s1
,但在调试器中,我在 float_array 中只得到一个值,即 5.0999945。没有其他索引具有任何其他值。正确转换测试变量值后,我在动态数组中看到了问题。
请问有解决办法吗?