Quick actions

cmd+k|ctrl+k

Navigation

Languages

Calc

Snippet info

Language

Cpp

Visibility

public

Author

tutor.the.code

Created

2017-08-06T17:57:34Z

Updated

2017-08-06T18:40:13Z

#include <iostream>
#include <string>
#include <cctype> // for tolower
using namespace std;
class Calculator
{

// You can directly init members with default values
// these members are not accessed from outside the calculator
// should be private
private:
string MenuResponse = ""; // consider this being of type char
int x = 0;
int y = 0;
int equal = 0; 

public:
Calculator() {}

// get the result
int result()
{
	return equal;
}

void Menu()
{
	cout << "For Addition press(A)\n";
	cout << "For Subtraction press(S)\n";
	cout << "For Multiplication press(M)\n";
	cout << "For Division press (D)\n";
	cout << "- ";
	cin >> MenuResponse;

	switch(tolower(MenuResponse[0]))
	{
		case 'a':
		cout << "Enter operands to add: ";
		cin >> x >> y;
		// save result of operation
		equal = Addition();
		break;
		// add the other cases for 'S', 'M' and 'D'
		//...
	}
	//...

}

// change the return value from void to
// the type result of the operation 'int'
// in this case
//void Addition()
	int Addition()
	{
		//move all the reading up to Menu
		//cout<<"Enter numbers to add\n";
		//cout << "Enter the first number:\n";
		//cin >> x;
		//cout << "Now enter the second number:\n";
		//cin >> y;
		//int equals = x+y; you don't need this


		return x+y;
	}

	// do the same as with Addition
	void Subtraction()
	{
		cout<<"Enter numbers to sub\n";
		cout << "Enter the first number:\n";
		cin >> x;
		cout << "Now enter the second number:\n";
		cin >> y;
		int equals = x-y;
		cout <<"="<<equals;
	}

	void Multiplication()
	{
		cout<<"Enter numbers to mult\n";
		cout << "Enter the first number:\n";
		cin >> x;
		cout << "Now enter the second number:\n";
		cin >> y;
		int equals = x*y;
		cout <<"="<<equals;
	}

	void Division()
	{
		cout<<"Enter numbers to div\n";
		cout << "Enter the first number:\n";
		cin >> x;
		cout << "Now enter the second number:\n";
		cin >> y;
		int equals = x/y;
		cout <<"="<<equals;
}
};
int main()
{

	Calculator Calc;
	Calc.Menu();

	cout << "Result: " << Calc.result() << endl;



		// Handle this inside Menu
		/******
		if (Calc.MenuResponse == "A")
		{
		Calc.Addition();
		}
		else if (Calc.MenuResponse == "S")
		{
		Calc.Subtraction();
		}
		else if (Calc.MenuResponse == "M")
		{
		Calc.Multiplication();
		}
		else if (Calc.MenuResponse == "D")
		{
		Calc.Division();
		}
		else
		{

		}
		*******/
}
INFO