Write a program reverse-order.cpp which asks the user to input two dates (earlier date then later date). The program should report the West basin elevationfor all days in the interval in the reverse chronological order (from the later date to the earlier).
Example:
$ ./reverse-order Enter earlier date: 05/29/2018 Enter later date: 06/02/2018 06/02/2018 590.22 ft 06/01/2018 590.23 ft 05/31/2018 590.24 ft 05/30/2018 590.26 ft 05/29/2018 590.32 ft
Hint: If for the previous tasks you did not use arrays, here you really have to read the data into arrays first, and only then report them in the required order.
********************************************************************USE THE FORMAT BELOW TO READ THE FILE*******************************
The datafile is a plain text file whose first line is a header followed by rows of data. The entries in each row are separated by the tab symbol, hence the name of the file format: TSV (tab-separated-values). It is the most convenient format for reading by our C++ program.
Each row has five fields: Date, Storage (in billions of gallons) and Elevation (in feet) for the East basin and for the West basin of the reservoir:
Date EastStorage EastElevation WestStorage WestElevation 01/01/2018 59.94 574 32.67 574.33 01/02/2018 59.89 573.99 32.57 574.29 01/03/2018 59.89 573.97 32.44 574.24 01/04/2018 59.9 573.97 32.22 574.07 ...
To read the datafile, we have to open an input file stream (represented by an object of type ifstream, here we called it fin):
ifstream fin("Current_Reservoir_Levels.tsv"); if (fin.fail()) { cerr << "File cannot be opened for reading." << endl; exit(1); // exit if failed to open the file }
Remember that the first line in the file is a header line. We have to skip it before we get to process the actual data. We can do that by reading that line into a temporary variable that we can call junk:
string junk; // new string variable getline(fin, junk); // read one line from the file
After that, the file can be read line by line. The most idiomatic C++ way to read such well-formatted file until the end would be the following:
while(fin >> date >> eastSt >> eastEl >> westSt >> westEl) { // this loop reads the file line-by-line // extracting 5 values on each iteration fin.ignore(INT_MAX, '\n'); //skips to the end of line, //ignorring the remaining columns // for example, to print the date and East basin storage: cout << date << " " << eastSt << endl; }
Here, variable date can be of type string, and the others are numeric variables of type double extracting the storage and elevation in East and West basins.
Answer : please upvote if its up to your expectation or comment for any query i will update the same. Thanks.
please check output Data and File Data in Attached Image.
Program Plan : Objective is Find West Basin Elevation between Earlier and Later Date from File
1. Get User Input Earlier Date and Later Date
2. Open File or not found error
3. Read Date from File and compare with Earlier and Later Date
4. if Matched print Date and West Basin Elevation else read next Date untill EOF(End of File)
5. Repeat Step 3 to 4.
Program :
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
string FindWestElevation(string str,char c); //Find Last
occurence of character
int GetMonth(string Date); //get int value of month from date
int GetDay(string Date); //get int value of day from date
int GetYear(string Date); //get int value of day from year
int main()
{
string LaterDate,EarlierDate; //string to save input from user
Later ,earlier
cout<<"Enter Earlier Date ";
cin>>EarlierDate;
cout<<"Enter Later Date ";
cin>>LaterDate;
ifstream fin("Current_Reservoir_Levels.tsv"); //open the
file
if (fin.fail()) { //if file not found or unable to open throw error
and exit
cerr << "File cannot be opened for reading." <<
endl;
exit(1); // exit if failed to open the file
}
else
{
// new string variable
while(fin)
{
string junk; //first chunk header of file
getline(fin,junk);
int pos=junk.find(" "); //find date ->parse using space
string date=junk.substr(0,pos); //store it in Date which is file
Date
int FileMonth=GetMonth(date); //Int value
int FileDay=GetDay(date); //Int value
int FileYear=GetYear(date); //Int value
// cout<<"FileDate"<<FileMonth<<" "<<FileDay<<" "<<FileYear<<endl;
int EarlierMonth=GetMonth(EarlierDate); //Int Value of Eariler
Date
int EarlierDay=GetDay(EarlierDate);
int EarlierYear=GetYear(EarlierDate);
// cout<<"EarilerDate"<<EarlierMonth<<" "<<EarlierDay<<" "<<EarlierYear<<endl;
int LaterMonth=GetMonth(LaterDate); //Int value of Later
Date
int LaterDay=GetDay(LaterDate);
int LaterYear=GetYear(LaterDate);
// cout<<"LaterDate"<<LaterMonth<<"
"<<LaterDay<<" "<<LaterYear<<endl;
if((FileMonth>=EarlierMonth) && (FileDay>=EarlierDay)
&& (FileYear>=EarlierYear)) //File Date greater than or
equal to file Date
{
if((FileMonth<=LaterMonth) && (FileDay<=LaterDay)
&& (FileYear<=LaterYear)) //File Date less than or equal
to File Date
{
cout<<date<<" "; //if true print Date
string WestElevation=FindWestElevation(junk,' '); //Find West
Elevation
cout<<WestElevation.append(" ft")<<endl; //append ft
and print Data
}
else
{
// cout<<"False1"<<endl;
}
}
else
{
//cout<<"False"<<endl;
}
}
}
}
string FindWestElevation(string str,char c) //return string and
find last occurence of character
{
int index = -1;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == c)
index = i;
}
string sub=str.substr(index+1); //get substring from last occurence to end of string
return sub;
}
int GetMonth(string Date)
{
int Month=0;
int pos=Date.find('/'); //first occurence of / charcter
Date=Date.substr(0,pos); //string from 0 to first occurence
Month=atoi(Date.c_str()); //string to const *pointer array and
convert to integer
return Month;
}
int GetDay(string Date)
{
int Day=0;
int pos=Date.find('/'); //find first occurence
Date=Date.substr(pos+1);
pos=pos=Date.find('/'); //find second occurence
Date=Date.substr(0,pos);
Day=atoi(Date.c_str()); //
return Day;
}
int GetYear(string Date)
{
string Year =FindWestElevation(Date,'/'); //find last occurence of
charcter
int year=atoi(Year.c_str()); //get integer value
return year;
}
Output :
Get Answers For Free
Most questions answered within 1 hours.