Need help please!

Hi all,

I have been trying to read a comma separated .txt file into an array and print it to console in C++. The txt file consists of two columns of double type data. For some reason the program runs, but gives blank output in the console. I want to know if I am doing something wrong. So far this is what I have:


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

using namespace std;

int main()
{
int i=0;
double x[10];
double y[10];
string line;
ifstream is;
is.open("UNTITLED.txt");

while(!getline(is,line).eof())
{
is >> x[i];
is >> y[i];

i++;

}
is.close();
for(int i=0;i<10;i++){
cout << "x=" << x[i] << "y= " << y[i] << endl;
}
return 0;
}


Last edited on
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
9
10
while(!getline(is,line).eof()) // what if the stream is in an invalid state?
while(getline(is,line)) // equivalent to -> cplusplus.com/reference/iostream/ios/good/
{
        is >> x[i];
        is.ignore(numeric_limits<streamsize>::max(), ','); // ignore the comma, #include <limits>
        is >> y[i];

        i++;

}
Topic archived. No new replies allowed.