Need help guys!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Need help guys!

  1. #1
    Join Date
    Oct 2012
    Posts
    2

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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Need help guys!

    Quote Originally Posted by gfer View Post
    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.

  3. #3
    Join Date
    Oct 2012
    Posts
    2

    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?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Need help guys!

    Quote Originally Posted by gfer View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center