A Simple Calculator

Vagabond 0 Tallied Votes 152 Views Share

Beginner C++ - A simple calculator writen in C++, compiled on Tubo C++ v4.5 & Borland C++ 5.5. The user inputs numbers & chooses '+','-','*'/' The operator is tested. If correct the appropriate sum is preformed.

//**********************************************************************
//Name : Calculator.cpp
//
//Programmer : Vagabond           
//                : Newbie
//Compiler : Borland Turbo C++ v4.5 & Borland BCC5.5
//
//Comments: 14/03/2006
//             
//
//**********************************************************************

#include <iostream.h>
#include <conio.h>      // for getch() & clrscr()
#include <ctype.h>      // for toascii()

int main()
{
	char  oppo = 'O';
	int   i = 0;
	int   first = 0;
	int   second = 0;
	int   answer = 0;
	const int ESC = 27;
	int   key = 0;

	while(key != ESC)
	{  
		cout << "\n\n\n SIMPLE CALCULATOR v1.0";
		cout << "\n\n Enter Your No's.  :  ";
		cin  >> first >> oppo >> second;
		i = toascii(oppo);
		while(i != 42 && i != 43 && i != 45 && i != 47)
		{
			cout << "\nERROR - Invalid Entry!\nOnly use +, -, * or / ";
			cin  >> oppo;
			i = toascii(oppo);
		}

		switch(i)
		{
			case 43 : answer = first + second;
							cout << "Total              :  " << answer;
							break;
			case 45 : answer = first - second;
							cout << "Total              :  " << answer;
							break;
			case 42 : answer = first * second;
							cout << "Total              :  " << answer;
							break;
			default : answer = first / second;
							cout << "Total              :  " << answer;
							break; 
		}
		cout << "\n\nPress any key to continue or Esc to Exit...";
		key = getche();
		clrscr();
	}
	clrscr();
	cout << "\n\n\nProgram Terminated by User";
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.