spelled out number calculator

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 7

#include "std_lib_facilities.h"

vector<string> numbers;

void initNumbers() {
	numbers.push_back("zero");
	numbers.push_back("one");
	numbers.push_back("two");
	numbers.push_back("three");
	numbers.push_back("four");
	numbers.push_back("five");
	numbers.push_back("six");
	numbers.push_back("seven");
	numbers.push_back("eight");
	numbers.push_back("nine");
}

int getNum() {
	int vali = numbers.size();
	string vals;

	// getInt
	if (cin >> vali) {
		if (vali <= 10) {
			return vali;
		}
		else {
			error("Entered integer is too high. You entered: ", vali);
		}
	}

	cin.clear();
	// getString
	string s = "noNumber";
	cin >> vals;
	for (int i = 0; i < numbers.size(); i++) {
		if (numbers[i] == vals) {
			return i;
			s = vals;
		}
	}
	if (s == "noNumber") {
		error("Illegal entered number. You entered: ", vals);
	}
}

int main() 
try{

	initNumbers();
	int value1 = 0;
	int value2 = 0;
	char operat = ' ';

	while (cin) {

		cout << "Enter two intgeres followed by a operator seperated by spaces.\n";
		//cout << "Enter one integer as digit or written out: \n";
		value1 = getNum();

		//cout << "Enter another integer as digit or written out: \n";
		value2 = getNum();

		//cout << "Enter an ooperator: \n";
		cin >> operat;

		if (operat == '+') {
			cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 + value2 << "\n";
		}
		if (operat == '-') {
			cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 - value2 << "\n";
		}
		if (operat == '/') {
			cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 / value2 << "\n";
		}
		if (operat == '*')
		{
			cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 * value2 << "\n";
		}
	}

	keep_window_open(".");
}

catch (runtime_error e) {	// this code is to produce error messages; it will be described in Chapter 5
	cout << e.what() << '\n';
	keep_window_open(".");	// For some Windows(tm) setups
}
catch (...) {	// this code is to produce error messages; it will be described in Chapter 5
	cout << "exiting\n";
	keep_window_open(".");	// For some Windows(tm) setups
}
Output:
Enter two intgeres followed by a operator seperated by spaces.
two 3 +
2 + 3 equals to: 5
Enter two intgeres followed by a operator seperated by spaces.

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.