|
-
October 16th, 2012, 04:11 PM
#1
Need help guys!
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;
}
-
October 16th, 2012, 04:23 PM
#2
Re: Need help guys!
 Originally Posted by gfer
Hi all,
I have been trying to read a comma separated .txt file into an array and print it to console in C++.
Please use code tags when posting code.
Code:
while(!getline(is, line).eof())
{
is >> x[i];
is >> y[i];
i++;
}
It is "line" that has the data. What your code does is read data into line using getline(), and then reads data again using operator >>. Are you not convinced that the data wasn't read the first time using getline()? If you debugged your code, you should have seen this. Are you using the debugger?
You should be taking "line" and then parse "line" into your tokens.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 16th, 2012 at 04:25 PM.
-
October 16th, 2012, 04:40 PM
#3
Re: Need help guys!
Thank you for replying. I am sorry for not using code tags.
I am using XCode 4.5.1 with the debugger.
How would I take "line" and parse it?
-
October 16th, 2012, 08:13 PM
#4
Re: Need help guys!
 Originally Posted by gfer
Thank you for replying. I am sorry for not using code tags.
I am using XCode 4.5.1 with the debugger.
How would I take "line" and parse it?
http://www.cplusplus.com/faq/sequences/strings/split/
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|