#include <iostream>
#include <string>
 
using namespace std;
 
double area_of_triangle(double base, double height) {
	double area;
 
	area = 0.5*base*height;
	return area;
}
 
double area_of_rectangle(double base, double height) {
	double area;
 
	area = base*height;
	return area;
}
 
double area_of_house(double base, double height) {
	double area, roof_height;
 
	roof_height = base * 0.5;
	area = area_of_triangle(base, roof_height) + area_of_rectangle(base, height);
	return area;
}
 
double volume_of_house(double base, double height, double depth) {
	return area_of_house(base, height)*depth;
}
 
void add_house(double &total, double base, double height, double depth) {
	total += volume_of_house(base, height,depth);
}
 
int main() {
	bool more = true;
	string request;
	double neighborhood_total = 0;
 
	while (more) {
		double base, height, volume, depth, area;
 
		cout << "\nchoose one of the following:\n";
		cout << endl;
		cout << "T compute the area of a triangle\n";
		cout << "R compute the area of a rectangle\n";
		cout << "H compute the area of a house\n";
		cout << "V compute the volume of a house\n";
		cout << "A compute the volume of a house, and add it to the neighborhood\n";
		cout << endl;
		cout << "S to stop\n";
 
		getline(cin, request);
 
		if (("T" == request) || ("t" == request)) {
 
			cout << "Base: ";
			cin >> base;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Height: ";
			cin >> height;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
 
			area = area_of_triangle(base, height);
 
			cout << "The area is " << area << endl;
		}
		else if (("R" == request) || ("r" == request)) {
 
			cout << "Base: ";
			cin >> base;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Height: ";
			cin >> height;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
 
			area = area_of_rectangle(base, height);
 
			cout << "The area is " << area << endl;
		}
		else if (("H" == request) || ("h" == request)) {
 
			cout << "Base: ";
			cin >> base;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Height: ";
			cin >> height;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
 
			area = area_of_house(base, height);
 
			cout << "The area is " << area << endl;
		}
		else if (("V" == request) || ("v" == request)) {
 
			cout << "Base: ";
			cin >> base;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Height: ";
			cin >> height;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Depth: ";
			cin >> depth;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
 
			volume = volume_of_house(base, height, depth);
 
			cout << "The volume is " << volume << endl;
		}
		else if (("A" == request) || ("a" == request)) {
 
			cout << "Base: ";
			cin >> base;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Height: ";
			cin >> height;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Depth: ";
			cin >> depth;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
 
			add_house(neighborhood_total, base, height, depth);
 
			cout << "The neighborhood total is " << neighborhood_total << endl;
		}
		else if (("S" == request) || ("s" == request)) {
			more = false;
		}
		else {
			cout << "Invalid choice, try again\n";
		}
	}
 
	cout << "The total for the neighborhood is " << neighborhood_total << ".\n";
 
	system("pause");
	return 0;
}
cs-142/house-functions.txt · Last modified: 2014/10/14 17:07 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0