SOURCE FILE: conepai2.cpp


//******************************************************************
// ConePaint program
// This program computes the cost of painting traffic cones in
// each of three different colors, given the height and diameter
// of a cone in inches, and the cost per square foot of each of
// the paints, all of which are input from a file
//******************************************************************
#include <iostream>
#include <iomanip>    // For setw() and setprecision()
#include <cmath>      // For sqrt()
#include <fstream>    // For file I/O

using namespace std;

const float INCHES_PER_FT = 12.0;   // Inches in 1 foot
const float PI = 3.14159265;        // Ratio of circumference
                                    //   to diameter
int main()
{
    float    htInInches;     // Height of the cone in inches
    float    diamInInches;   // Diameter of base of cone in inches
    float    redPrice;       // Price per square foot of red paint
    float    bluePrice;      // Price per square foot of blue paint
    float    greenPrice;     // Price per square foot of green paint
    float    heightInFt;     // Height of the cone in feet
    float    diamInFt;       // Diameter of the cone in feet
    float    radius;         // Radius of the cone in feet
    float    surfaceArea;    // Surface area in square feet
    float    redCost;        // Cost to paint a cone red
    float    blueCost;       // Cost to paint a cone blue
    float    greenCost;      // Cost to paint a cone green
    ifstream inData;         // Holds cone size and paint prices
    ofstream outData;        // Holds paint costs

    outData << fixed << showpoint;         // Set up floating-pt.
                                           //   output format
    // Open the files

    inData.open("cone.dat");
    outData.open("results.dat");

    // Get data

    inData >> htInInches >> diamInInches >> redPrice
           >> bluePrice >> greenPrice;

    // Convert dimensions to feet

    heightInFt = htInInches / INCHES_PER_FT;
    diamInFt = diamInInches / INCHES_PER_FT;
    radius = diamInFt / 2.0;

    // Compute surface area of the cone

    surfaceArea = PI * radius *
                  sqrt(radius*radius + heightInFt*heightInFt);

    // Compute cost for each color

    redCost = surfaceArea * redPrice;
    blueCost = surfaceArea * bluePrice;
    greenCost = surfaceArea * greenPrice;

    // Output results

    outData << setprecision(3);
    outData << "The surface area is " << surfaceArea << " sq. ft."
            << endl;
    outData << "The painting cost for" << endl;
    outData << "   red is" << setw(8) << redCost << " dollars"
            << endl;
    outData << "   blue is" << setw(7) << blueCost << " dollars"
            << endl;
    outData << "   green is" << setw(6) << greenCost << " dollars"
            << endl;
    return 0;
}