Need help with reading data from a file while loop

We just started class today and im having major memory blockage when it comes to programming even though the last class was only a few months ago...




Problem description: The text file DJI.txt contains the information of a stock market index --
Dow Jones Industrial Average Index (DJI for short) -- for a continuous time
in year 2008. Each line contains the date and the DJI's value on that day.
For the format of the file, please view DJI.txt.

Please write a program that reads DJI values stored in the data file DJI.txt,
and finds out the date with highest DJI value during this period stored in the file.
*/

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main( )
{
const string inputFile = "/var/www/html/programs2/2170_1_5/dataFiles/DJI.txt";
ifstream ifs; //an input file stream

int day; //Today's day
int maxDay; //The day of the date with max. DJI
string month; //Today's month
string maxMonth; //The month of the date with max. DJI
double maxDJI; //The max. DJI value got so far.
double dji; //Today's DJI

ifs.open( inputFile.c_str() ); //open the file

//INSERT WHILE LOOP HERE
// this is what i have come up with so far

ifs>>month>>day>>dji;
while (ifs)
{

if (maxDJI > dji)
{
maxDJI=dji;
maxDay=day;
maxMonth=month;
}
ifs>>month>>day>>dji;
}














ifs.close(); //close the input file

cout << "DJI is highest on " << maxMonth << " " << maxDay << " with " << maxDJI << endl;

return 0;
}





and here is the text file


May 28 : 12594.0303

May 29 : 12593.8701

May 30 : 12638.3203

June 2 : 12503.8203

June 3 : 12402.8496

June 4 : 12390.4805

June 5 : 12604.4502

June 6 : 12209.8096
you'll want to run the while() loop as far as you have not reached the end of the DJI.txt file, right?
i think fstream can check which line of the external file you are at the moment.
yea it needs to check all the numbers and dates given to make sure its got the highest
Topic archived. No new replies allowed.