In: Computer Science
Write a program compare.cpp that asks the user to input two dates (the beginning and the end of the interval). The program should check each day in the interval and report which basin had higher elevation on that day by printing “East” or “West”, or print “Equal” if both basins are at the same level.
Example:
$ ./compare Enter starting date: 09/13/2018 Enter ending date: 09/17/2018 09/13/2018 West 09/14/2018 West 09/15/2018 West 09/16/2018 West 09/17/2018 West
Explanation:
Date | East (ft) | West (ft) | |
---|---|---|---|
09/13/2018 | 581.94 | 582.66 | West is higher |
09/14/2018 | 581.8 | 582.32 | West is higher |
09/15/2018 | 581.62 | 581.94 | West is higher |
09/16/2018 | 581.42 | 581.55 | West is higher |
09/17/2018 | 581.16 | 581.2 | West is higher |
Here i am providing the answer. Hope it helps. Please give me a like, it helps me a lot.
#include<cstdlib>
#include<fstream>
#include<iostream>
using namespace std;
int main() {
string startDate;
cout << "Enter starting date: ";
cin >> startDate;
string endDate;
cout << "Enter ending date: ";
cin >> endDate;
ifstream
fin("Current_Reservoir_Levels.tsv");
if (fin.fail()){
cerr<<"File cannot
be opened for reading."<<endl;
exit(1);
}
string junk;
getline(fin, junk);
int dateRange = 0;
string date;
double eastSt, eastEl, westSt, westEl;
while(fin >> date >> eastSt >>
eastEl >> westSt >> westEl) {
if(date ==
startDate){dateRange = 1;}
if(date ==
endDate){dateRange = 0;}
if(dateRange ==
1){
if (eastEl>westEl) {cout<< date <<" East
"<<endl;}
else if(eastEl<westEl){cout << date << " West
"<<endl;}
else {cout << date << " Equal "<<endl;}
}
}
fin.close();
}
Thank you. please like.