Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help code this in c++ the assignment starts below the dashed line ----------------------------------------------------------------------------------------------------------- Temperature Readings File Read section 10.11: Reading a Structured File Read

please help code this in c++ the assignment starts below the dashed line

-----------------------------------------------------------------------------------------------------------

Temperature Readings File

  • Read section 10.11: Reading a Structured File
  • Read the Powerpoints from lecture for the temperature reading project
  • Using the code given from the book and Powerpoints, define each function specified in the attached Temperatures.h in Temperatures.cpp
  • Define the << output operator for Year:
    • Loop through each month, day, and hour
    • If the temperature for that hour has been read then output it
    • You will have to use multiple dot operators to access the nested vector elements
    • Set your precision to 2 and each field width to 12
ostream& operator<<(ostream& os, Year& y); 
  • Define the helper function to convert an index to a printable month, to be used in your Year output operator:
string int_to_month(int i); 
  • Use the attached main.cpp to do the following:

    • Read the attached input file temps.txt
    • Use your << operator to output each Year to both cout and an output file named out.txt
  • temps.txt:

{year 2017 {month feb (1 1 68.3456 ) (2 3 66.6646 ) ( 1 0 67.2789)} {month dec (15 15 -9.2010 ) (15 14 -8.8405) (14 0 -2.0001)} } {year 2018 {month jan (30 7 62.1244 ) (5 23 61.4789 ) ( 7 11 68.6241)} {month mar (14 19 -4.2154 ) (19 9 -1.8567) (22 4 -6.876)} } 
  • Output after reading temps.txt, going to both the console and out.txt:
 2017| February| 1| 0| 67.28| 2017| February| 1| 1| 68.35| 2017| February| 2| 3| 66.66| 2017| December| 14| 0| -2.00| 2017| December| 15| 14| -8.84| 2017| December| 15| 15| -9.20| 2018| January| 5| 23| 61.48| 2018| January| 7| 11| 68.62| 2018| January| 30| 7| 62.12| 2018| March| 14| 19| -4.22| 2018| March| 19| 9| -1.86| 2018| March| 22| 4| -6.88|

The files are below:

Main.cpp

#include "Temperatures.h"

int main()

{

// open an input file :

cout << "Please enter input file name ";

string iname;

cin >> iname;

ifstream ifs {iname};

if (!ifs) error("can't open input file ",iname);

ifs.exceptions(ifs.exceptions()|ios_base::badbit); // throw for bad()

// open an output file :

cout << "Please enter output file name ";

string oname;

cin >> oname;

ofstream ofs {oname};

if (!ofs) error("can't open output file ",oname);

// read an arbitrary number of years:

vector ys;

while(true)

{

Year y; // get a freshly initialized Year each time around

if (!(ifs>>y)) break;

ys.push_back(y);

}

cout << "read " << ys.size() << " years of readings ";

for (Year& y : ys) ofs << y;

for (Year& y : ys) cout << y;

}

Temperatures.h:

#ifndef TEMPERATURES_H_INCLUDED

#define TEMPERATURES_H_INCLUDED

#include "std_lib_facilities.h"

const int not_a_reading = -7777; // less than absolute zero

const int not_a_month = -1;

///constexpr initializes at compile time

constexpr int implausible_min = -200;

constexpr int implausible_max = 200;

struct Day

{

///initialize hours to a nameless vector with 24 values of not_a_reading

///must use {} initialization list syntax

vector hours {vector(24,not_a_reading)};

};

struct Month

{ // a month of temperature readings

int month {not_a_month}; // [0:11] January is 0 for array indexing

vector days {32}; // [1:31] one vector of readings per day, skip 0

};

struct Year

{ // a year of temperature readings, organized by month

int year; // positive == A.D.

vector months {12}; // [0:11] January is 0

};

struct Reading

{

int day;

int hour;

double temperature;

};

istream& operator>>(istream& is, Reading& r);

istream& operator>>(istream& is, Month& m);

istream& operator>>(istream& is, Year& y);

///Print each reading for every month, day, and hour within Year& y

///Use a triple nested for loop for 12 months, 31 days per month, and 24 hours per day

///print each reading in the following format:

///set os precision to 2, fixed point

///field width 12, separated by '|'

/// year| Month| day| hour| reading|

///example:

/// 2018| January| 30| 7| 62.12|

///The member access operator . is your friend

///string int_to_month(int i) to convert a month index to a printable string

///only print if the reading != not_a_reading

ostream& operator<<(ostream& os, Year& y);

///helper functions

///Convert "jan", "feb", to 0, 1,

///Use an array of strings

int month_to_int(string s);

///Check ranges on day, hour, temperature

bool is_valid(const Reading& r);

///Clear the fail state of the input stream for more reading

///Look for the terminating '}'

void end_of_loop(istream& ist, char term, const string& message);

///Convert an index to a printable month, uses an array of strings

string int_to_month(int i);

///index to string conversion arrays

const vector month_input_tbl =

{

"jan", "feb", "mar", "apr", "may", "jun", "jul",

"aug", "sep", "oct", "nov", "dec"

};

const vector month_print_tbl =

{

"January", "February", "March", "April", "May", "June", "July",

"August", "September", "October", "November", "December"

};

#endif // TEMPERATURES_H_INCLUDED

Temperatures.cpp:

#include "Temperatures.h"

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Why would an organisation conduct a diversity audit? Give examples.

Answered: 1 week ago

Question

What is operatiing system?

Answered: 1 week ago