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;
}
}