COMP 1671: Intro to Computer Science

 

Lab 9: File Processing

 

Lab Activity

The National Weather Service publishes data files containing daily temperature and precipitation information*.  Each data file contains the daily information for a particular month and location.  We will be using the data from November 2001 to practice manipulating data files in C++.

 

The file named  weather.dat” contains the required weather information.  You can download the file by right-clicking on this link: weather.dat and choosing “Save target as”.  Be sure to save the file to your Cygwin home directory.  Each line of the data file contains:

 

 

Someone started writing a program to produce a monthly report containing basic weather information.  Finish the program so that it will do the following:

 

 

Here are some sample lines from the input data file:

 

1    56    35   0.05

2    76    65   0.00

3    75    56   0.03

 

Here is the incomplete program:


 

#include <fstream>

#include <iostream>

 

using namespace std;

 

int main()

{

  // Create the input and output file streams

 

  // Open the input and output files

 

  // Declare variables for storing information

  // from the input data file

  int dayNumber;

  int maxTemp;

  int minTemp;

  float dayPrecip;

 

  // Declare variables for calculated information

  float dayAverageTemp;

  float totalPrecip;

 

  // Write the column headings for the output

  // file

  outData << " Day MaxTemp MinTemp AverageTemp" << endl;

  outData << "---------------------------------" << endl;

 

  // Loop while there is still information in the file

  while (  )

  {

     // Get the day number, maximum temperature, minimum

     // temperature and amount of precipitation from the

     // input data file.

 

     // Calculate the day's average temperature

     dayAverageTemp =

 

     // Add the day's precipitation to the total monthly precipitation

 

     // Write the day number, maximum temperature, minimum

     // temperature, and average temperature to the output

     // data file.

 

  }

 

  // Write the total monthly precipitation to the output file

 

  // Close the input and output data files

 

  return 0;

}

 

 



* This information was taken from http://www.crh.noaa.gov/den/index.html