Advertisement
Hobospider

Untitled

Feb 7th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. //Main Code
  2. #include <iostream>
  3. #include <cstring>
  4. #include <iomanip>
  5. #include "Weather.h"
  6. using namespace std;
  7. using namespace sict;
  8. double findLow(const char* date, const Weather *data, int dataSize) {
  9. int i, o;
  10. for (i = 0; i < dataSize; i++) {
  11. o = strcmp(data[i].date(), date);
  12. cout << o << date << data[i].date() << endl;
  13. if (strcmp(data[i].date(),date) == 0)
  14. return data[i].low();
  15. }
  16. return 0.0;
  17. }
  18.  
  19. int main(){
  20. int i; // loop counter
  21. int n; //the count of days worth of weather
  22. // initialize the weather pointer here
  23. Weather* weather = nullptr;
  24.  
  25. cout << "Weather Data\n";
  26. cout << "=====================" << endl;
  27. cout << "Days of Weather: ";
  28. cin >> n;
  29. cin.ignore();
  30. // allocate dynamic memory here
  31. weather = new Weather[n];
  32.  
  33. for (i = 0; i < n; i++){
  34. char date_description[7];
  35. double high;
  36. double low;
  37.  
  38. // ... add code to accept the user input
  39. cout << "Enter date: ";
  40. cin >> date_description;
  41. cout << "Enter high: ";
  42. cin >> high;
  43. cout << "Enter low : ";
  44. cin >> low;
  45. weather[i].set(date_description, low, high);
  46. // for the weather array
  47.  
  48. }
  49. cout << endl
  50. << "Weather report:" << endl
  51. << "Date high low" << endl
  52. << "======================" << endl;
  53.  
  54. for (i = 0; i < n; i++){
  55. weather[i].display();
  56. }
  57.  
  58. char query[7];
  59. cout << endl << "Enter the date you are looking for: ";
  60. //accept user input for the date to find
  61. cin >> query;
  62. //(in this example stored in char query[7])
  63. // and display the found low temprature.
  64.  
  65. cin.getline(query, 7, '\n');
  66. double low = findLow(query, weather, n);
  67. cout << "Low temperature: " << low << endl;
  68. // deallocate dynamic memory here
  69. delete[] weather;
  70. weather = nullptr;
  71.  
  72. return 0;
  73.  
  74. }
  75. //=============
  76. //Header file
  77. #ifndef SICT_WEATHER_H_
  78. #define SICT_WEATHER_H_
  79.  
  80. /*Weather.h*/
  81.  
  82. // sict namespace
  83. namespace sict {
  84. class Weather {
  85. char _dateDescription[7];
  86. double _highTemp;
  87. double _lowTemp;
  88.  
  89. public:
  90. void set(const char* date, double low, double high);
  91. void display() const;
  92. const char* date() const;
  93. double low() const;
  94. // member function
  95. };
  96. }
  97. #endif
  98. //===========
  99. //Weather Class file
  100. #include <iostream>
  101. #include <cstring>
  102. #include <iomanip>
  103. #include "Weather.h"
  104. using namespace std;
  105.  
  106.  
  107. namespace sict {
  108.  
  109. void Weather::set(const char* date, double low, double high) {
  110. strcpy(_dateDescription, date);
  111. _lowTemp = low;
  112. _highTemp = high;
  113. }
  114. void Weather::display() const {
  115. cout << left << setw(10) << setfill('_')
  116. << _dateDescription << right << fixed
  117. << setprecision(1) << setw(6) << setfill('_')
  118. << _highTemp << right << fixed << setprecision(1)
  119. << setw(6) << setfill('_') << _lowTemp << endl;
  120. }
  121. const char* Weather::date() const {
  122. return _dateDescription;
  123. }
  124. double Weather::low() const {
  125. return _lowTemp;
  126. }
  127.  
  128. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement