# Newbie Console Calculator with C++ Classes

Ok, the classes story seems to be running everyone crazy. Here is a teeth bite of the much you can do with classes… If you are interested, you can  go through the code and try new values for yourself. Classes are not that difficult after all!

/* Project: Simple Console Calculator with C++ Developer: Ebenezer Obasi Website: www.eobasi.com */ #include <iostream> using namespace std; class calc{ int x, y; public: int values(int,int); int add(){return (x+y);} int sub(){return (x-y);} int mult(){return (x*y);} int div(){return (x/y);} }; int calc::values(int a, int b){ x = a; y = b; } int main() { const string first = “Enter First Number: “; const string second = “Enter Second Number: “; const string choOpp = “Enter your sign: “; const string closeT = “Enter ‘e’ to close or ‘c’ to continue: “; int a, b; char opp, close; do{ cout << first; cin >> a; cout << second; cin >> b; cout << choOpp; cin >> opp; calc start; start.values(a, b); if( opp == ‘+’){ double display = start.add(); cout << “Answer is: ” << display << endl; } else if( opp == ‘-‘){ double display = start.sub(); cout << “Answer is: ” << display << endl; } else if( opp == ‘*’){ double display = start.mult(); cout << “Answer is: ” << display << endl; } else if( opp == ‘/’){ double display = start.div(); cout << “Answer is: ” << display << endl; } else{ string display = “Invalid operator! n t Use +, -, * or /”; cout << display << endl; } cout << closeT; cin >> close; if( close == ‘e’){ string display = “********************************************nGoodbyen” ; cout << display << endl; } }while(close != ‘e’); return 0; }

If you are using visual studio do not forget to include the following line at the top of the page.

#include “stdafx.h”

This program is written with Code Block compiler and also tested on visual studio. I hope you find it useful.
Have fun!