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.
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 :)