Newbie Console Calculator with C++ Classes

Last updated on May 5th, 2018 at 02:48 pm

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!

Did you get the answer you were searching for?

Save hours of searching online or wasting money testing unnecessary plugins, get in touch with me and let's discuss a suitable plan for your project. Best thing about this service is that you are never placed on hold and get to talk to an expereinced Oxwall/Skadate developer.

Get Answers for Free!

Ask a question related to this topic and get immediate answers from other community members in 48hrs or less. Contribute by answering members questions.

Ask Question
Premium Service

Whether it's a custom plugin, theme or dedicated support needed to get you started on your project, get professional 24/7 support tailored to your need.

Get in Touch

Or just leave a comment...

Leave a Reply