1

I have created a program that reads a CSV file with 2 columns where except first row remaining rows are numerical values. After reading it writes values to 2 different arrays(x[],y[]). My problem is the value are reading perfect but the value that should be at x[0] is at x[-5] and at x[0] there is value of y[4].

below is the data in CSV file

Distance,Time
10,12
57,69
40,54
60,71
90,200
68,76

I have tried different ways to solve this but none of them worked. Please help me if possible.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream data("Data.csv");
    int Count = 0, x[Count+1], y[Count+1], a = 0;//it is behaving differently while removing a=0
    if(data.is_open())
    {

        string line, line1;

        //for first row
        getline(data,line,',');
        cout << line;
        getline(data,line1,'\n');
        cout << " " << line1 << endl;

        //remaining rows with numerical values
        while(data.good())
        {
            getline(data,line,',');
            x[Count] = stoi(line); //converting string to integer
            cout << x[Count];
            getline(data,line1,'\n');
            y[Count] = stoi(line1); //converting string to integer
            cout << " " << y[Count] << endl;
            Count++;
        }

        cout << "   " << Count << endl;
        cout << x[0] << endl;
        cout << y[0] << endl;
    }
    else
    {
       cout << "ERROR OCCURED WHILE OPENING THE FILE" << endl;
    }
}
Simon
  • 1,546
  • 2
  • 14
  • 37
sai vamsi
  • 57
  • 6
  • 5
    Two problems with your arrays: First of all C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array); Secondly arrays are fixed in size, and can't be extended. It seems you need to pick up [a decent book about C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn about e.g. [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Nov 11 '19 at 08:34
  • 1
    Do you realize, you initialize your array with the size of 1? – skratchi.at Nov 11 '19 at 08:35
  • 1
    If you read a good book, you should also learn that [`while (data.good())` is bad](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Some programmer dude Nov 11 '19 at 08:36
  • 1
    What is `x[-5]`? – Thomas Sablik Nov 11 '19 at 08:44

2 Answers2

1

Instead of int[] which has fixed size. Use a standard container like std::vector<int>. And instead of x[Count] = value; use the push_back() method.

#include <vector>
// ...
std::vector<int> x,y;
// ...
x.push_back(stoi(line));
y.push_back(stoi(line1));

int Count = x.size(); //or y.size() as they should be same

You could also use a vector of pairs instead of two vectors std::vector<std::pair<int, int>> or a vector of tuples std::vector<std::tuple<int, int>>

Some error checking should also be added...

slepic
  • 621
  • 4
  • 11
0

Here is a solution using std::istringstream and std::vector<std::pair<int, int>> for storing the result. The only real trick is replacing comma with space so it can be parsed with std::istringstream.

#include <fstream>
#include <vector>
#include <sstream>
//...

ifstream infile("Data.csv");

std::string line;
std::getline(infile, line);//skip header line
std::vector<std::pair<int, int>> coords;
while (std::getline(infile, line))
{
    std::replace(line.begin(), line.end(), ',', ' ');//replace ',' with space
    std::istringstream iss(line);
    int x, y;
    if (!(iss >> x >> y)) { 
        break; // error in format
    }
    coords.emplace_back(x, y);
}

You probably need to better handle the format error situation in real code. Also you could make the header optional if needed (skipping errors on the first line).

darune
  • 9,436
  • 1
  • 16
  • 47