money class 1.0

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 9 Exercise 14
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 9 Exercise 14

#include "std_lib_facilities.h"

class Money {
private:
	long int amount;
public:
	//constructors
	Money(double);
	
	//non-modifying operations
	long int get_amount() { return amount; }
	double get_amount_decimal();
	void print() { cout << get_amount_decimal() << endl; }

	//modifying operations
};

Money::Money(double a) {
	double temp_d0 = a;
	temp_d0 *= 100;
	int temp_i = (int)temp_d0;
	double difference = temp_d0 - temp_i;

	if (difference >= 0.5) {
		temp_i++;
	}

	amount = temp_i;
}

double Money::get_amount_decimal() {
	return double(amount) / 100;
}

void read_value(string& c, double& i) {
	string check;
	double input;

	if (cin >> input) {
		i = input;
	}
	else {
		cin.clear();
		cin >> check;
		c = check;
		if (check != "x") {
			cout << "Invalid input: " << check << endl;
		}
	}
}

void calculate() {
	while (true) {
		string check;
		double input;
		cout << "Enter dollar amount as float: " << endl;
		read_value(check, input);
		if (check == "x") {
			break;
		}
		else {
			Money dollar(input);
			dollar.print();
		}
	}
}

int main()
try
{
	calculate();
	keep_window_open();
}

catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Enter dollar amount as float:
34.5346
34.53
Enter dollar amount as float:
34.566
34.57
Enter dollar amount as float:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.