I am writing a program to read ain a file.

s axis -1.50000E+01 to -1.49000E+01
t axis -1.50000E+01 to -1.49000E+01
4.16402E-06 0.0077
repeat for new numbers (these values don't match those taken in below, I just pulled them out of a random part of the file)

the format doesn't really matter because I know it is reading in correctly. What does matter is what is happening to the data after I read it in

Some variables are not being affected by the operations done to them
see here

-14.9 -13.9 1 11 1 11 4.16402e-06 0.0077
-14.9 -13.8 1 12 1 11 4.16774e-06 0.0077
-14.9 -13.7 1 13 1 13 4.17141e-06 0.0077
-14.9 -13.6 1 14 1 13 4.17513e-06 0.0078
-14.9 -13.5 1 15 1 15 4.1789e-06 0.0078
-14.9 -13.4 1 16 1 16 4.18272e-06 0.0078

The 6th column should be the same as the fourth...
See the code below for details where is says //Map coordinate...

for (int pixel_number = 1; pixel_number <= 320; pixel_number++) {
    
    //Determine pixel coordinates
    In_file.ignore(INT_MAX, 's');
    In_file.ignore(INT_MAX, 's');
    In_file >> s_axis_1; 
    In_file.ignore(INT_MAX, '\n');
    
    In_file.ignore(256 ,'s');
    In_file >> t_axis_1;
    In_file.ignore (INT_MAX, '\n');

    //Get pixel value
    In_file >> temp_value;
    In_file.ignore();
    In_file >> temp_error;

    if (s_axis_1 == -3.65541E-14 || s_axis_1 == 3.65541E-14) {
      s_axis_1 = 0;
    }

    if (t_axis_1 == -3.65541E-14 || t_axis_1 == 3.65541E-14) {
      t_axis_1 = 0;
    }
    
    //Map coordiante of pixel to location in array
    a = (s_axis_1 + 15)*10;
    b = (t_axis_1 + 15)*10;
    m = (int)a;
    n = (int)b;

    //Store the values
    data[m][n].value = temp_value;
    data[m][n].error = temp_error;
    cout << s_axis_1 << "  " << t_axis_1 << "  " << a <<"  " << b << "  "
	 << m << "  " << n << "  " <<temp_value << "  " << temp_error <<endl;
  } 
 } else {
  cout << "Unable to open input file\n" << endl;
 } //End above if statment

 In_file.close();

Can anyone help??

Thanks in advance

Sorry but there is insufficient code here to run, test and generally play with,
a bit more of a working example, e.g. something which runs would have been good.

But there are several things that might be a problem:

(a) Given the lines a=(s_axis_1+15)*10; and the similar one for b, did you really mean this. As s_axis is likely to between
-150 and 150 so a is going to be about 1650. Do you have an array overrun??

(b) Did you intend to do the same thing with b, it seems like the *10 was when the data was a flat single array, and you might have been doing int index= a+b; (c) This code doesn't seem to be right: (s_axis_1== -3.65541E-14) . It makes absolutely no difference in the present code (since you cast a to an integer) and any program that uses double precision should not expect zero to be exactly zero, rounding error is a fact of computing.


If you still have a problem can you make the smallest possible test program, even
if you use stuff like double s_axis_1=3.132e3; as the set up etc.
It will help us help you

Edited 6 Years Ago by StuXYZ: n/a

Yea sorry about that, I should have been more clear.
As I said before, I am reading in a text file that has
two lines which define a spot in grid
and then one line that gives the value at that location and error of that value.

The grid spans from -15 to 15 for both axis. To map them into the array of [300][300]
that I made, I add 15 and then multiply by ten. (this is because there are ten pixels per cm)

So for instance if the file said:

s axis -1.46000E+01 to -1.45000E+01
t axis -1.49000E+01 to -1.48000E+01
4.16402E-06 0.0077

s_coordinate would become -1.46E01
then int m would be (-14.6 + 15)*10 = 4
I do the same thing for the t axis

For some reason sometimes this doesn't work, like here.
This is the cout from below in the code.
It doesn't make sense to me because it sets variable b to the right numbers, but when I convert from float to int, variable n remains unchanged.

I am getting no out of bounds error or anything else during runtime.


-14.9 -13.9 1 11 1 11 4.16402e-06 0.0077
-14.9 -13.8 1 12 1 11 4.16774e-06 0.0077
-14.9 -13.7 1 13 1 13 4.17141e-06 0.0077
-14.9 -13.6 1 14 1 13 4.17513e-06 0.0078
-14.9 -13.5 1 15 1 15 4.1789e-06 0.0078
-14.9 -13.4 1 16 1 16 4.18272e-06 0.0078

//Removed library calls and function declarations

//Define storage structure for data
struct Pixel_info {
  float value;
  float error;
};

//Initialize pixel storage array of structs
Pixel_info data[300][300];

//Initialize variables for parsing;
 string input_name, output_name;
 int start_pixel_info = 0;
 int px_per_cm = 10;
 int grid_offset = 15;


  for (int pixel_number = 1; pixel_number <= 900; pixel_number++) {
    float s_coordinate, t_coordinate;
    float a, b, value, error;
    int m, n;
    
    //Determine pixel coordinates
    In_file.ignore(INT_MAX, 's');
    In_file.ignore(INT_MAX, 's');
    In_file >> s_coordinate; 
    In_file.ignore(INT_MAX, '\n');
    
    In_file.ignore(256 ,'s');
    In_file >> t_coordinate;
    In_file.ignore (INT_MAX, '\n');

    //Get pixel value
    In_file >> value;
    In_file.ignore();
    In_file >> error;
 

    //Here is where the problem seems to be

    //Map coordiante of pixel to location in array
    a = (s_coordinate + grid_offset)*px_per_cm;  //Grid offset=15
    b = (t_coordinate + grid_offset)*px_per_cm;  //px_per_cm=10
    m = (int)a;
    n = (int)b;
    
    cout << s_coordinate << "  " << t_coordinate << "  " << a <<"  " << b << "  "
	  << m << "  " << n << "  " << value << "  " << error <<endl;
  } 
 } else {
  cout << "Unable to open input file\n" << endl;
 } //End above if statment

See how on the fourth line of the data that was printed out that b is 14, but n is 13?? Why is that happening?

nevermind, i solved my problem. It turns out that the values I was reading in not completely precise. When I stored them they became things like 15.0000000001 or 14.999999997. Just wrote a rounding function to fix them and set the precision for the floating point output to 15 spaces.

This question has already been answered. Start a new discussion instead.