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!

A reliable hosting company which I highly recommend and use for Wordpress, Oxwall and Skadate hosting is currently giving out more than 50% discount, including VPS and shared hosting. This might be your last chance to host with a2hosting.
/* 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!

Premium Service

If you seek a custom feature developed for your website or need 24/7 premium support to help you get started with your social network or Skadate dating website, save hours of searching online or wasting money on 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 in Touch

Leave a Reply