0

I'm trying to read a csv file that has the following contents:

1.00E+00,0.00E+00,5.70E-01

2.00E+00,5.00E-04,5.70E-01

3.00E+00,1.00E-03,5.70E-01

etc.

However, only the second and third elements of each row are read and stored into the arrays I pass the values to. I can't find what is wrong with the code. If I remove the while statement and repeat the lines under them for n times it works fine. Does anyone have a fix to this? Tired using fin.good() in the while conditions statement and it didn't work.

    #include <iostream>
    #include <string.h>
    #include <fstream>
    #include <iomanip>
    #include <vector>

    using namespace std;

    int main()
    {
    string xcord;
    string ycord;
    string mole;
    double m[100], tx[100], ty[100];
    int i, k;
    k = 0;

    fstream fin;
    fin.open("argon");

    while(!fin.eof())
    {
    getline(fin,xcord,',');
    getline(fin,ycord,',');
    getline(fin,mole,'\n');
    tx[k] = stod(xcord);
    ty[k] = stod(ycord);
    m[k] = stod(mole);
    k++;
    }

    for(i=0;i<k;i++)
    cout<<xcord<<"\t"<<ycord<<"\t"<<mole<<"\n";
    return 0;
    }

  • 1
    And when you used your debugger to run your program, what did you see? This is precisely what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Oct 25 '20 at 21:14
  • I haven't used a debugger before but will now! Thanks for your input!! – thermocoder Oct 26 '20 at 15:20

0 Answers0