0

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;
    }
}
4
  • 5
    Two problems with your arrays: First of all C++ doesn't have variable-length arrays; Secondly arrays are fixed in size, and can't be extended. It seems you need to pick up a decent book about C++ and learn about e.g. std::vector. Commented Nov 11, 2019 at 8:34
  • 1
    Do you realize, you initialize your array with the size of 1? Commented Nov 11, 2019 at 8:35
  • 1
    If you read a good book, you should also learn that while (data.good()) is bad. Commented Nov 11, 2019 at 8:36
  • 1
    What is x[-5]? Commented Nov 11, 2019 at 8:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.