65.8k views
1 vote
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 elevation for all days in the interval in the reverse chronological order (from the later date to the earlier).

1 Answer

5 votes

Answer:

reverse-order.cpp

#include<iostream>

#include <fstream>

#include <cstdlib>

#include <climits>

#include <sstream>

using namespace std;

#include <vector>

int main()

{

ifstream fin("Current_Reservoir_Levels.tsv");

if (fin.fail())

{//check whether file exists or not

cerr << "File cannot be opened for reading." << endl;

exit(1);

}

//declare two vectors

vector<string> Date;

vector<float> westElVec;

string header;

getline(fin, header); // read one line from the file

string dateArr[365], date;

double eastSt, eastEl, westSt, westEl;

string date1, date2;

cout << "Enter starting date: ";

cin >> date1; // getting starting date from user

cout << "Enter ending date: ";

cin >> date2; // getting ending date from user

int count = 0;

while (fin >> date >> eastSt >> eastEl >> westSt >> westEl)

{

fin.ignore(INT_MAX, '\\'); //skips to the end of line,

//get the record from file

//check if data is between the start and end or not

if (date1 <= date && date2 >= date)

{//insert the data

Date.push_back(date);

westElVec.push_back( westEl);

count++;

}

}

//sort the data by date indecending order

for (int i = 0; i < count; ++i)

{

for (int j = 0; j < count - i - 1; ++j)

{

// Comparing consecutive dates

if (Date[j] < Date[j + 1])

{

////swap West basin elevation

//double twestElVec = westElVec[j];

//westElVec[j] = westElVec[j + 1];

//westElVec[j + 1] = twestElVec;

//swap dates

string tDate = Date[j];

Date[j] = Date[j + 1];

Date[j + 1] = tDate;

}

}

}

for (int i = 0; i < count; i++)

cout << Date[i] << "\t" << westElVec[i] <<"ft"<< endl;

fin.close();

//system("pause");

return 0;

}

Step-by-step explanation:

User Stracka
by
6.5k points