Project 3

Using the cpi.csv data, write a program that will read from the command line the name of a file. Read this file into your program. Request a uyear on the command line. Optionally request from the user on the command line an amount.
. Check that you have enough command line input. Stop with a message if you don’t have enough command line values.

Once the data have been read, check that the requested year is in range.

The ratio of any two years is an estimate of the change in the cost of living. Compute the change in the cost of living from the year you specify to 2020. Print it out neatly with some informative text. Print the result to 2 decimal places.

In 1954 a color television cost $1295. From your result how much would that be in 2020 dollars?

A rough estimate of the year-over-year inflation rate can be obtained from

inflation[i]=(cpi[i+1]-cpi[i])/12.

Compute the inflation rate for the data and print with the corresponding year to a comma-separated file. Use any plotting package with which you are familiar (Excel, Matlab, Python, R, etc.) to plot the data.

Sample solution

/*****************************************************************************
Author: K. Holcomb
Date:   February 2017

File Name:   inflation.cxx

Description: This program computes inflation factors between two years.  
             
Input:       Inflation data from a file, and one or two years.
             
Output:      Inflation factor between a specified year and 2020
******************************************************************************/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char **argv)
{
    //Declare Variables
    string inFile, inputYear1="", inputAmount="";
    ifstream fin;
    int year1=0;
    float amount=0;
    string line;

    //Get input
    if ( argc==3 ) {
       inFile=argv[1];
       inputYear1=string(argv[2]);
       stringstream ss1(inputYear1);
       ss1>>year1;
    }
    else if ( argc==4) {
       inFile=argv[1];
       inputYear1=string(argv[2]);
       stringstream ss1(inputYear1);
       ss1>>year1;
       inputAmount=string(argv[3]);
       stringstream ss2(inputAmount);
       ss2>>amount;
    }
    else {
       cout<<"Usage: datafile year <amount>\n";
       return 1;
    }

    //Read data.  Should do more checking here.
    fin.open(inFile.c_str());
    int lineCount=0;
    
    if ( fin.is_open() ) {
        while (getline(fin,line)) {
            lineCount++;
        }
    }

    fin.close();
    fin.open(inFile.c_str());

    //Data arrays (one header line)
    int nyears=lineCount-1;
    int   * year=new int[nyears];
    float * cpi =new float[nyears];

    //Skip header
    getline(fin,line);

    int lineNo=0;
    while ( getline(fin,line) ) {
        stringstream lineStream(line);
        string * lineVals=new string[2];
        int index=0;
        while ( getline(lineStream, lineVals[index],',') ) {
            ++index;
        }
        year[lineNo]=atoi(lineVals[0].c_str());
        cpi[lineNo]=atof(lineVals[1].c_str());
        lineNo++;
    }

    if (year1<year[0] || year1 > year[nyears-1]) {
        cout<<"Requested year outside data range."<<endl;
        return 2;
    }

    int baseYear=year[0];

    int index=year1-baseYear;
    float factor=cpi[nyears-1]/cpi[index];

    if (amount==0) {
    cout<<"The inflation factor since "<<year1<<" is "
        <<setiosflags(ios::fixed)<<setprecision(2)
        <<factor<<".\n";
    }
    else {
    cout<<amount<<" in "<<year1<<" is "
        <<setiosflags(ios::fixed)<<setprecision(2)
        <<amount*factor<<" in current dollars.\n";
    }

    //Make file
    float * inflation = new float[nyears-1];

    for (int n=0; n<nyears-1; ++n) {
        inflation[n]=(cpi[n+1]-cpi[n])/12.;
    }

    ofstream fout;
    fout.open("inflation.csv");
    for (int i=0;i<nyears-1;++i) {
        fout<<year[i]<<","<<inflation[i]<<"\n";
    }

    return 0;
}

Previous
Next