/* Lab 5 assignment
---------------------------------------------
Patrick Schmid pds2
February 1, 2006
Lab 5 solution
Lab instructor: Patrick Schmid
Lab section: 10 & 11
Purpose: Calculate the volume of a pyramid. Read base and height from
		 a text file. Output to screen and a text file. Output either
		  in cubic feet, cubic inches or cubic yards
Algorithm: Volume = Base * Height / 3
		   1 cubic meter= 35.31*cubic feet
		   cubic feet --> cubic inch, multiply by 1728
		   cubic feet -> cubic yard, divide by 27
*/

#include <iostream.h>
#include <fstream.h>

void main() {
	//declare variables
	double base; //base in square meters
	double height; //height in meters
	double volume; //volume in cubic meters

	//open files
	ifstream indata1 ("pyramids.txt", ios::in);
	ofstream outdata1 ("results.txt", ios::out);
	
	//read from file
	indata1>>base>>height;

	//echo print input
	cout<<"Base (square meters): "<<base<<endl;
	cout<<"Height (meters): "<<height<<endl;
	outdata1<<"Base (square meters): "<<base<<endl;
	outdata1<<"Height (meters): "<<height<<endl;

	//check variables
	if (height>0 && base>0) {

		//calculate volume in meters
		volume=base*height/3;

		//output result to screen & file
		cout<<"Volume of pyramid (cubic meters): "<<volume<<endl;
		outdata1<<"Volume of pyramid (cubic meters): "<<volume<<endl;
		
		//convert volume into cubic feet
		volume*=35.31;

		//adjust units
		//if volume < 1  cubic foot, output in cubic inches
		//if volume >=1 cubic foot and volume <1 cubic yard, output in cubic feet
		//otherwise output in cubic yards
		if (volume<1) { //convert to cubic inches
			volume*=1728;
			cout<<"Volume of pyramid: "<<volume<<" cubic inches.";
			outdata1<<"Volume of pyramid: "<<volume<<" cubic inches.";
		}
		else if (volume<27) { //keep in cubic feet
			cout<<"Volume of pyramid: "<<volume<<" cubic feet.";
			outdata1<<"Volume of pyramid: "<<volume<<" cubic feet.";
		}
		else { //convert to cubic yards
			volume=volume/27;
			cout<<"Volume of pyramid: "<<volume<<" cubic yards.";
			outdata1<<"Volume of pyramid: "<<volume<<" cubic yards.";
		}
	}
	else { //show if user provided incorrect height or base
		cout<<"You entered invalid input.\n The program will now terminate.\n";
		outdata1<<"You entered invalid input.\n The program will now terminate.\n";
	}
	cout<<endl;
	outdata1<<endl;

	//close files
	indata1.close();
	outdata1.close();
}

