1

I have a data of nearly 1000 elements and want to read some of the columns into an array. the first two lines are the headers and I'd like to skip them. I also need to calculate the differences between some numbers two consecutive columns and display the result on a different column.

Data looks something like this

444,,, x-coordinates,y-coordinates,a,b
3.00,-4.00,2.00,6.00
0.67,-3.88,0.00,0.00

I want to have an output similar to
3.00 -4.00 6.00
0.67 -3.88 0.00

and so on

This is what I've tried so far.

After opening the file successfully and declaring variables:

getline(inFile, line, ',');//obtain number of rows
        getline(inFile, line, '\n');//skip first two lines
        getline(inFile, line, '\n');

                while (inFile.good())//
                {
                    
                    getline(inFile, temp, ',');//read first column seperated by ','
                    x[i] = stod(temp);
                    getline(inFile, temp, ',');//read second column seperated by ','
                    y[i] = stod(temp);
                    
                    getline(inFile, dummy, ',');

                    getline(inFile, temp, ',');//read third column seperated by ','
                    b[i] = stod(temp);

I get this as output

2.12242e-3146.95333e-3102.12239e-3146.95333e-310 7656.67 2.12235e-3147.90868e-3152.12239e-314 0 7656.67 1.39612e-3096.95323e-3102.12239e-314 0 7656.67 4.29837e-3226.95333e-3102.12239e-3143.95253e-323 7656.67 4.34778e-3227.90868e-3152.1224e-314 0 7656.67 6.95325e-3106.95319e-3102.1224e-314 0 7656.67 4.94066e-3246.95333e-3102.1224e-3142.12399e-314 7656.67 07.90868e-3152.1224e-314 0 7656.67 6.95333e-3106.95323e-3102.12241e-314 0 7656.67 2.12242e-3146.95333e-3102.12241e-3146.95313e-310 7656.67 2.12235e-3147.90868e-3152.12241e-314 0 7656.67 1.39612e-3096.95321e-3102.12241e-314 0 7656.67 3.6462e-3216.95333e-3102.12241e-314-3.58185e+263 7656.67 3.67585e-3217.94207e-3152.12242e-314 0 7656.67 6.95326e-3106.95319e-3102.12242e-314 0 7656.67 4.94066e-3246.95333e-3102.12244e-3146.95313e-310 7656.67 07.94207e-3152.12244e-314 0 7656.67 6.95333e-3106.95323e-3102.12245e-314 0 7656.67 2.12242e-3146.95333e-3102.12245e-3142.52962e-320 7656.67 2.12235e-3147.90868e-3152.12246e-314 0 7656.67 1.39612e-3096.95323e-3102.12239e-314 0 7656.67 5.92879e-3226.95333e-3102.12239e-3142.12399e-314 7656.67 5.92879e-3227.90868e-3152.12239e-314 0 7656.67 6.95326e-3106.95323e-3102.12239e-314 0 7656.67 4.94066e-3246.95333e-3102.12239e-3146.07701e-322 7656.67 07.90868e-3152.12239e-314

Mercy Dada
  • 11
  • 2
  • not that important for the first lines, but once you reach the last lines, this will be relevant: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – 463035818_is_not_a_number Mar 12 '21 at 14:18
  • 1
    Please be more specific than "something like this" (don't you know?) and "it's not working" (in what way?). And read about why your loop is wrong [here](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – molbdnilo Mar 12 '21 at 14:19
  • 1
    Assignment assigns the value on the right-hand side to the object on the left-hand side. You've got them in the wrong direction. – molbdnilo Mar 12 '21 at 14:20
  • calling `getline` twice is a valid way to skip the first two lines. Thats not the issue in your code – 463035818_is_not_a_number Mar 12 '21 at 14:20
  • Hint: use a struct or class to model the row, then overload `operator>>`. – Thomas Matthews Mar 12 '21 at 15:45
  • Search the internet for "C++ read CSV struct". Always search first. There a plethora of answers and similar questions to your post. – Thomas Matthews Mar 12 '21 at 15:46

1 Answers1

0

Here's how I would do this:

struct Record
{
  double x, y;
  double a, b;
  friend std::istream& operator>>(std::istream& input, Record& r);
};

std::istream& operator>>(std::istream& input, Record& r)
{
  char comma;
  input >> x;
  input >> comma;
  input >> y;
  input >> comma;
  input >> a;
  input >> comma;
  input >> b;
  input.ignore(1000, '\n');
  return input;
}

Your input code could look like this:

std::vector<Record> database;
Record r;
while (input_file >> r)
{
  database.push_back(r);
}

To access row 5, column 2:

double value = database[4].y;

This solution uses a struct to model the elements in a row (a.k.a. record).
Each record is read in and appended to a database.

The data items can be accessed through the database.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144