Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello. I needed some help with writing a program. You can edit anything wrong wi

ID: 3555682 • Letter: H

Question

Hello. I needed some help with writing a program. You can edit anything wrong with it. I already constructed the program, but I need the rest of it written. Any help would be appreciated. Also, no foriegn syntaxes (following the book: Problem Solving w/ C++ by Savitch W.).

Files can be downloaded from here:

http://www.4shared.com/rar/VP5r89_xba/Waher12.html
http://www.4shared.com/rar/pW678E6Nba/Waher_Assign.html

Send your answer to: nova1700@yahoo.com

I'll be checking my email to see your answer. Post in the comments to let me know that you sent an answer.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;

class WeatherObs {
private: // Items in private
   string location;
   int year, month, day;
   int high, low;
   double precipitation;

public: // Items in public
   WeatherObs(); // Default constructor
   WeatherObs (string NewLocation, int NewHigh, int NewLow, int NewYear, int NewMonth, int NewDay, double NewPrecipitation);
   void setLocation(string NewLocation);
   void setHigh(int NewHigh);
   void setLow(int NewLow);
   void setprecipitation(double NewPrecipitation);
   void setDate(int NewYear, int NewMonth, int NewDay);
   string getLocation();
   int getYear();
   int getMonth();
   int getDay();
   int getHigh();
   int getLow();
   double getPrecipitation();
   string getMonthName();
   int getMeanTemp();
   void input(istream& ins);
   void output(ostream& outs);
};


/*********************************************************************************************************************************************/

WeatherObs::WeatherObs() {
   location = "????";
   year = 999;
   month = 999;
   day = 999;
   high = 999;
   low = 999;
   precipitation = -9.99;
}

WeatherObs::WeatherObs (string NewLocation, int NewHigh, int NewLow, int NewYear, int NewMonth, int NewDay, double NewPrecipitation) { //2nd constructor with parameters
   low = high = 999;
   year = month = day = 999;
   precipitation = -9.99;
  
   setLocation(NewLocation);
   setHigh(NewHigh);
   setLow(NewLow);
   setprecipitation(NewPrecipitation);
   setDate(NewYear, NewMonth, NewDay);
}

void WeatherObs::setLocation(string NewLocation) { // Mutators
   location = NewLocation;
}

void WeatherObs::setHigh(int NewHigh) {
   if (NewHigh >= 0)
       high = NewHigh;
   else
   cout << "Illegal temp. Keeping default high temp." << endl;
}

void WeatherObs::setLow(int NewLow) {
   if (NewLow >= 0)
       low = NewLow;
   else
       cout << "Illegal temp. Keeping default low temp." << endl;
}

void WeatherObs::setprecipitation(double NewPrecipitation) {
   if (NewPrecipitation >= 0)
       precipitation = NewPrecipitation;
   else
       cout << "Illegal precipitation amount. Keeping default precipitation amount." << endl;
}

void WeatherObs::setDate(int NewYear, int NewMonth, int NewDay) {
   if ((NewYear < 0 || NewMonth < 0 || NewMonth < 0))   {
       cout << "Illegal calendar value(s). Keeping default value." << endl;
       return;
   }
   year = NewYear;
   month = NewMonth;
   day = NewDay;
}

string WeatherObs::getLocation() { // Accessors
   return location;
}

string WeatherObs::getMonthName() {
   switch(month) {
       case 1:
           return "January";
       case 2:
           return "February";
       case 3:
           return "March";
       case 4:
           return "April";
       case 5:
           return "May";
       case 6:
           return "June";
       case 7:
           return "July";
       case 8:
           return "August";
       case 9:
           return "September";
       case 10:
           return "October";
       case 11:
           return "November";
       case 12:
           return "December";
   };
   return "";
}

int WeatherObs::getHigh() {
   return high;
}

int WeatherObs::getLow() {
   return low;
}

int WeatherObs::getMeanTemp() {
   int mean;
   mean = low + (high - low + 1)/2;
   return mean;
}

int WeatherObs::getYear() {
   return year;
}

int WeatherObs::getMonth() {
   return month;
}

int WeatherObs::getDay() {
   return day;
}

double WeatherObs::getPrecipitation() {
   return precipitation;
}

void WeatherObs::input(istream & ins) { // Input member function

   string tempLocation;
   int tempYear;
   int tempMonth;
   int tempDay;
   int tempHigh;
   int tempLow;
   double tempPrecipitation;

   ins >> tempLocation;
   setLocation(tempLocation);

   ins >> tempYear;
   ins >> tempMonth;
   ins >> tempDay;
   setDate(tempYear, tempMonth, tempDay);

   ins >> tempHigh;
   setHigh(tempHigh);

   ins >> tempLow;
   setLow(tempLow);

   ins >> tempPrecipitation;
   setprecipitation(tempPrecipitation);

}

void WeatherObs::output(ostream& outs) { // Output member function
   outs.setf(ios::fixed);
   outs.setf(ios::showpoint);
   outs.precision(2);
      
   outs << "Weather Observation for " << location << " on " << getMonthName() << " " << day << ", " << year << endl;
   outs << "High: " << high << " degrees F" << endl;
   outs << "Low: " << low << " degrees F" << endl;
   outs << "Mean: " << getMeanTemp () << " degrees F" << endl;
   outs << "Precipitation: " << precipitation << " in." << endl;
}

/*********************************************************************************************************************************************/
int main () {
   ifstream climateData;

   climateData.open("climateData.txt");
   if (climateData.fail())
   {
       cout << "Error opening input file. ";
       exit(1); // 1 indicates an error
   }


   WeatherObs Data1;
   WeatherObs Data2;
   WeatherObs Data3;
   WeatherObs Data4;

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   Data1.input(climateData);

   Data2.input(climateData);

   Data3.input(climateData);

   Data4.input(climateData);
  
   cout << endl;
   Data1.output(cout);
   cout << endl;

   Data2.output(cout);
   cout << endl;

   Data3.output(cout);
   cout << endl;

   Data4.output(cout);
   cout << endl;

   climateData.close();
   return 0;
}