Question: // Program name: Midterm2 // // Description: This program reads a location's temperature data // and prints a report. // // Author: Write your name

// Program name: Midterm2 // // Description: This program reads a location's temperature data // and prints a report. // // Author: Write your name here . . . // // Date: Write today's date mm/dd/yyyy . . . //

#include #include #include #include #include

using namespace std;

// Constants you need to use for output formatting // The width of the LOCATION NAME column const int COL1 = 14; // The width of each of the temperature columns const int COLT = 4; // The width of the average temperature column const int COLA = 6;

// Constants used for validating temperatures const int MIN_TEMP = -50; const int MAX_TEMP = 150;

// The LocationData data structure and // related functions. const int LOCNAME_SIZE = 12; const int TEMPS_SIZE = 4; struct LocationData { // Location's name char locName[LOCNAME_SIZE]; // Location's temperatures int temps[TEMPS_SIZE]; };

// Functions related to the LocationData data structure bool readLocationData(LocationData *ptr); const char *getLocationName(const LocationData *ptr); double getAverageTemp(const LocationData *ptr);

bool isValidTemp(const LocationData *ptr); void printHeading(); string formatLocationData(const LocationData *ptr);

int main() { // The main function tests the functions related to the LocationData // by reading location data into a LocationData variable.

// Create location variables LocationData location1; // Read location data bool isValid = readLocationData(&location1); // Print the location temperature report if (isValid) { // Print the report heading printHeading(); // Print the location data cout << formatLocationData(&location1) << endl; } else { // The location's data is invalid cout << "The location contains invalid data." << endl; } return 0; }

//***************************************************************************** //* Given a pointer to a LocationData data structure, //* return the location's name contained in the data structure. //* //* Parameters //* ptr - pointer to a LocationData struct //* //* Returns //* The location's name. //***************************************************************************** const char *getLocationName(const LocationData *ptr) { const char *name = nullptr; // Write your code with comments . . . return name; }

//***************************************************************************** //* The function named readLocationData receives a pointer parameter that points //* to a LocationData data structure to be filled with a location's data. //* //* It reads the location's name and some temperature numbers from //* the keyboard and stores the name in the location's name field and //* the temperatures in the temps array. //* //* This function expects a string and some integer numbers from the keyboard //* all entered in one line delimited by commas. //* //* If the read is successful and the input is validated, the function //* returns true. Otherwise it returns false and the input LocationData //* data structure is unchanged. //* //* Parameters //* ptr - pointer to a LocationData struct //* //* Returns //* true if successful or false otherwise. //***************************************************************************** bool readLocationData(LocationData *ptr) { bool isValid = false; // Create a temporary LocationData for receiving the keyboard input LocationData temp; // Read one line from the keyboard and save it. string input; getline(cin, input); if (cin) { // Use a stringstream object to help the input process stringstream sstr; sstr.str(input); // Use the input stream to // retrieve data from the line of input. // Retrieve the location name string locname; getline(sstr, locname, ','); // Store the location name into the locName field of temp // as a C-string. // Write your code with comments . . . // Loop to retrieve the temperatures and store into the the // temps array of temp. for (int index = 0; index < TEMPS_SIZE; ++index) { string temstr; getline(sstr, temstr, ','); if (sstr) { temp.temps[index] = stoi(temstr); } } // Check for input success and then validate if (sstr && isValidTemp(&temp)) { // Fill the given data structure // with data from the input line. *ptr = temp; isValid = true; } } return isValid; }

//***************************************************************************** //* Get the average temperature of the given location. //* //* Parameters //* ptr - pointer to a LocationData struct //* //* Returns //* The average temperature. //***************************************************************************** double getAverageTemp(const LocationData *ptr) { double avg = 0.0; // Write your code with comments . . . return avg; }

//***************************************************************************** //* Verify the given location info is valid. That is, //* all temperatures are in the range between MIN_TEMP and MAX_TEMP inclusive. //* //* Parameters //* //* ptr - a pointer to LocationData. //* //* Returns //* true if the info is valid or false otherwise. //***************************************************************************** bool isValidTemp(const LocationData *ptr) { bool isValid = true; // Write your code with comments . . .

return isValid; }

//***************************************************************************** //* Print the report heading. //* //* Parameters //* //* Returns //* void //***************************************************************************** void printHeading() { // Print the labels at the top of each column cout << left << setw(COL1) << "LOC Name"; for (int index = 0; index < TEMPS_SIZE; ++index) { // Use a stringstream object to prepare // a header label that starts with 'T' // followed by a number. stringstream sstr; sstr << 'T' << index + 1; cout << right << setw(COLT) << sstr.str(); } cout << right << setw(COLA) << "Avg" << endl; // Print a line under the labels cout << setfill('-'); cout << setw(COL1 + COLT * TEMPS_SIZE + COLA) << '-' << endl; cout << setfill(' '); }

//***************************************************************************** //* Format the location's name, temperatures and average temperature //* into a string. //* //* Parameters //* ptr - pointer to a LocationData struct //* //* Returns //* The string that contains the formatted location data without the //*. newline character at the end. //***************************************************************************** string formatLocationData(const LocationData *ptr) { string result; // Write your code with comments . . .

return result; }

/* Test input 1 Guerneville, 73, 74, 76, 76 Expected output LOC Name T1 T2 T3 T4 Avg ------------------------------------ Guerneville 73 74 76 76 74.8 Test input 2

Guerneville, 73, 174, 76, 76 Expected output The location contains invalid data. */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!