read column of double into an array

closed account (zbREy60M)
Hello guys, I am trying to read a .txt file that has a column of numbers into an array, the file looks like this:

1
2
3
4
5
6
7
8
9
.28
1.03
.21
2.6
2.3
1.0
1.0
.156
.4
and so on...

I've been trying to read it into an array of strings and then cast it to a double. However, I am having no luck when doing this. Is there an alternative way to read the file, so I just read it directly to an array of doubles, and skip the string step?

This is the code I've been using, and I get errors when trying to cast the array from strings to floats. I would like to skip the string step entirely if possible...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    string dmArray[123];
	double DMArray[123];

	ifstream dmfile ("DM.txt");

	if (dmfile.is_open())
		{
			for (int i = 0; i < 123; i++)
				{
				getline (dmfile,dmArray[i]);
				}
			dmfile.close();
			DMArray[123] = atof(dmArray[123].c_str());
			cout << DMArray[123] << endl;
		}
	 else cout << "unable to open DM file";


Quick idea, is it because I am calling it DMArray[123] instead of DMArray[i]? I don't think so, I still get errors, but just an idea. Thank you very much.
The main problem is that DMArray[123] calls the 124th value in an array of 123. That's a problem. To make that line work you'd have to do this:

for (int i = 0; i < 123; i++) DMArray[i] = atof(dmArray[i].c_str());

Skipping the string step is also easy!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    double DMArray[123];
    ifstream dmfile("DM.txt");
    if (dmfile.is_open())
    {
        for(int i = 0; i < 123; i++)
        {
            dmfile >> DMArray[i];
            cout << DMArray[i];
        }
        dmfile.close();
    }
    else
        cout << "unable to open DM file";
}


If you don't know the number of elements, you can use vectors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    vector<double> DMArray;
    ifstream dmfile("DM.txt");
    while (dmfile.good())
    {
        double temp;
        dmfile >> temp;
        DMArray.push_back(temp);
    }

    for (int i = 0; i < DMArray.size(); i++)
        cout << DMArray[i] << endl;
}
closed account (zbREy60M)
Thank you very much. I now have a follow up question.

I need to transfer these into a different set of coordinates. To do this I made a new array that is supposed to be the sine value of one of the arrays. However, when I run it, it gives me weird values that is not the sine of the number in the array.

How do I take the sin of an array?

I tried:

1
2
3
4
for (int i = 0; i < 124; i++)
	{
		SINLONGAArray[i] = sin(LONGAArray[i]);
		cout << SINLONGAArray[i] << "\n";

And it gives me a different value than what my calculator gives me. Also, is the sin function in C++ in degrees or radians? I need it in degrees. Thank you :)
in C++ sin accepts radians. You'll need to do this to convert to rad
1
2
for (int i = 0; i < 124; i++)
    SIMLONGArray[i] = sin(LONGAArray[i] * 0.017453);
Topic archived. No new replies allowed.