SOURCE FILE: canvas.cpp


//******************************************************************
// Canvas program
// This program computes the dimensions and costs of materials
// to build a painting canvas of given dimensions. The user is
// asked to enter the length and width of the painting and the
// costs of the wood (per inch) and canvas (per square foot)
//******************************************************************
#include <iostream>
#include <iomanip>    // For setprecision()

using namespace std;

const float SQ_IN_PER_SQ_FT = 144.0;    // Square inches per
                                        //   square foot
int main()
{
    float length;              // Length of painting in inches
    float width;               // Width of painting in inches
    float woodCost;            // Cost of wood per inch in dollars
    float canvasCost;          // Cost of canvas per square foot
    float lengthOfWood;        // Amount of wood to buy
    float canvasWidth;         // Width of canvas to buy
    float canvasLength;        // Length of canvas to buy
    float canvasAreaInches;    // Area of canvas in square inches
    float canvasAreaFeet;      // Area of canvas in square feet
    float totCanvasCost;       // Total cost of canvas being bought
    float totWoodCost;         // Total cost of wood being bought
    float totCost;             // Total cost of materials

    cout << fixed << showpoint;              // Set up floating pt.
                                             //   output format
    // Get length and width

    cout << "Enter length and width of painting:" << endl;
    cin >> length >> width;

    // Get wood cost

    cout << "Enter cost per inch of the framing wood in dollars:"
         << endl;
    cin >> woodCost;

    // Get canvas cost

    cout << "Enter cost per square foot of canvas in dollars:"
         << endl;
    cin >> canvasCost;

    // Compute dimensions and costs

    lengthOfWood = (length + width) * 2;
    canvasWidth = width + 5;
    canvasLength = length + 5;
    canvasAreaInches = canvasWidth * canvasLength;
    canvasAreaFeet = canvasAreaInches / SQ_IN_PER_SQ_FT;
    totWoodCost = lengthOfWood * woodCost;
    totCanvasCost = canvasAreaFeet * canvasCost;
    totCost = totWoodCost + totCanvasCost;

    // Print dimensions and costs

    cout << endl << setprecision(1);
    cout << "For a painting " << length << " in. long and "
         << width << " in. wide," << endl;
    cout << "you need to buy " << lengthOfWood << " in. of wood,"
         << " and" << endl;
    cout << "the canvas must be " << canvasLength << " in. long"
         << " and " << canvasWidth << " in. wide." << endl;

    cout << endl << setprecision(2);
    cout << "Given a wood cost of $" << woodCost << " per in."
         << endl;
    cout << "and a canvas cost of $" << canvasCost
         << " per sq. ft.," << endl;
    cout << "the wood will cost $" << totWoodCost << ',' << endl;
    cout << "the canvas will cost $" << totCanvasCost << ','
         << endl;
    cout << "and the total cost of materials will be $" << totCost
         << '.' << endl;
    return 0;
}