computing yields for gilts using c++

Hi, I'm a complete beginner at c++, and I have an urgent assignment to complete, I have to read a text file safely using c++, however I'm getting no output, below is my code,

my output is supposed to be like this:

7 06 11 2001 103.184457 06 05 2001 06 04 2001

7 07 06 2002 104.148962 07 06 2001 07 12 2001

7.25 07 12 2007 114.565710 07 06 2007 07 06 2008

7.5 07 12 2006 114.202459 07 06 2006 07 06 2007

7.75 08 09 2006 112.757901 08 03 2006 08 03 2007

8 10 06 2003 108.100383 10 01 2003 10 12 2003

8 25 09 2009 120.455691 25 03 2009 25 03 2010

8 27 09 2013 127.931492 27 03 2013 27 03 2014

8 07 12 2015 133.745956 07 01 2015 07 06 2016

8 07 06 2021 142.045956 07 01 2021 07 06 2022

8.5 07 12 2005 116.359454 07 06 2005 07 06 2006

8.75 25 08 2017 142.771304 25 02 2017 25 02 2018

9 06 08 2012 135.548587 06 02 2012 06 02 2013
please help?

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string getcontent;
ifstream openfile("data.txt");
if(openfile.is_open())
{
while(! openfile.eof())
{
getline(openfile, getcontent);
cout << getcontent << endl;
}
}

system("pause");
return 0;
}
Try changing this

1
2
3
4
5
6
7
8
if(openfile.is_open())
{
while(! openfile.eof())
{
getline(openfile, getcontent);
cout << getcontent << endl;
}
}


to this

1
2
while (getline(openfile, getcontent))
    cout << getcontent << endl;
It looks like ifstream openfile can not open the specified file. You can check whether the problem is that the file can not be found by placing it in the root directory and using

ifstream openfile("c:\\data.txt");
Thank you so much for the help, is just that I placed the text file in the wrong directory :)
Topic archived. No new replies allowed.