Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main Code
- #include <iostream>
- #include <cstring>
- #include <iomanip>
- #include "Weather.h"
- using namespace std;
- using namespace sict;
- double findLow(const char* date, const Weather *data, int dataSize) {
- int i, o;
- for (i = 0; i < dataSize; i++) {
- o = strcmp(data[i].date(), date);
- cout << o << date << data[i].date() << endl;
- if (strcmp(data[i].date(),date) == 0)
- return data[i].low();
- }
- return 0.0;
- }
- int main(){
- int i; // loop counter
- int n; //the count of days worth of weather
- // initialize the weather pointer here
- Weather* weather = nullptr;
- cout << "Weather Data\n";
- cout << "=====================" << endl;
- cout << "Days of Weather: ";
- cin >> n;
- cin.ignore();
- // allocate dynamic memory here
- weather = new Weather[n];
- for (i = 0; i < n; i++){
- char date_description[7];
- double high;
- double low;
- // ... add code to accept the user input
- cout << "Enter date: ";
- cin >> date_description;
- cout << "Enter high: ";
- cin >> high;
- cout << "Enter low : ";
- cin >> low;
- weather[i].set(date_description, low, high);
- // for the weather array
- }
- cout << endl
- << "Weather report:" << endl
- << "Date high low" << endl
- << "======================" << endl;
- for (i = 0; i < n; i++){
- weather[i].display();
- }
- char query[7];
- cout << endl << "Enter the date you are looking for: ";
- //accept user input for the date to find
- cin >> query;
- //(in this example stored in char query[7])
- // and display the found low temprature.
- cin.getline(query, 7, '\n');
- double low = findLow(query, weather, n);
- cout << "Low temperature: " << low << endl;
- // deallocate dynamic memory here
- delete[] weather;
- weather = nullptr;
- return 0;
- }
- //=============
- //Header file
- #ifndef SICT_WEATHER_H_
- #define SICT_WEATHER_H_
- /*Weather.h*/
- // sict namespace
- namespace sict {
- class Weather {
- char _dateDescription[7];
- double _highTemp;
- double _lowTemp;
- public:
- void set(const char* date, double low, double high);
- void display() const;
- const char* date() const;
- double low() const;
- // member function
- };
- }
- #endif
- //===========
- //Weather Class file
- #include <iostream>
- #include <cstring>
- #include <iomanip>
- #include "Weather.h"
- using namespace std;
- namespace sict {
- void Weather::set(const char* date, double low, double high) {
- strcpy(_dateDescription, date);
- _lowTemp = low;
- _highTemp = high;
- }
- void Weather::display() const {
- cout << left << setw(10) << setfill('_')
- << _dateDescription << right << fixed
- << setprecision(1) << setw(6) << setfill('_')
- << _highTemp << right << fixed << setprecision(1)
- << setw(6) << setfill('_') << _lowTemp << endl;
- }
- const char* Weather::date() const {
- return _dateDescription;
- }
- double Weather::low() const {
- return _lowTemp;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement