To create a Calculator in C ++

By kedinn 2013/09/08

We begin to create a simple exercise of a calculator, where we will use the 4 basic operations of algebra.
We will do it step by step, the first part of the code that we must place are the bookstores of Windows and the bookstores to use the operations of basic entrance and exit:

#include #include

Fact this, we left from our main structure, that is the following one:

int main () \ {return 0;}

Now if we see, for this we need two entrances (numbers to use) to receive data from the keyboard we used the function cin.
We declare the variables and we received them from the keyboard:

int num1, num2 and head of cattle;
cout << “Enters I number 1: ?; cin >> num1;
cout << “enters I number 2: ?; cin >> num2;

\ n is to give line jump
With this already we asked for both numbers that we will use, now only remains to conduct the operations and to show them in screen:

head of cattle = num1+num2;
cout << “\ nLa sum is: ? << head of cattle;

The exit would be something thus
The sum is: head of cattle
The rest is simple, we only accommodated the code previous to the other operations.

head of cattle = num1 – num2;
head of cattle = num1/num2;
head of cattle = num1 * num2;

The code in the end, would be of the following way.

#include <stdlib.h> #include <iostream> using namespace STD;
int main () \ {int num1, num2, head of cattle;
cout << “Enters I number 1: ?;
cin >> num1;
cout << “enters I number 2: ?;
cin >> num2;
head of cattle = num1+num2;
cout << “the sum of? << num1 << “+ “<< num2 << “is “<< head of cattle;
head of cattle = num1-num2;
cout << “the subtraction of? << num1 << “- “<< num2 << “is “<< head of cattle;
head of cattle = num1/num2;
cout << “the division of? << num1 << “between “<< num2 << “is “<< head of cattle;
head of cattle = num1*num2;
cout << “the multiplication of? << num1 << “x “<< num2 << “is “<< head of cattle;
return 0;}

In the console we would have left something thus:

It enters I number 1: 8
it enters I number 2: 7
The sum is: 15
The subtraction is: 1
The division is: 1
The multiplication is: 56
It presses a key to continue…

Calculator with menu in c++

Calculator a little but outpost but nothing difficult.

 

#include using namespace STD;

int main () \ {double n1, n2; int opc;

cout << “************************************* \ n? << “* basic Calculator for 2 numbers * \ n? << “************************************* \ n? << endl << endl << endl << “Enters the first one I number: ?; cin >> n1;
cout << endl << endl << “Enters the second I number: ?; cin >> n2;

system (“cls?);

cout << “************************************* \ n? << “* basic Calculator for 2 numbers * \ n? << “************************************* \ n? << endl << endl << endl <<? That operation wishes to realise? “<< endl << endl << “ 1) Sum. \ n? << “ 2) Subtraction. \ n? << “ 3) Multiplication. \ n? << “ 4) Division. \ n? << “*) presses any other I number to leave \ n \ n \ n? <<? Enters I number of the option that wishes to realise: “; cin >> opc;
switch (opc) \ {marries 1:

system (“cls?);

cout <<? \ n \ n the sum of the numbers is: “<< n1+n2 << endl << endl;
break;
it marries 2:
system (“cls?);
cout <<? \ n \ n the subtraction of the numbers is: “<< n1-n2 << endl << endl;

break;
it marries 3:
system (“cls?);
cout <<? \ n \ n the multiplication of the numbers is: “<< n1*n2 << endl << endl;
break;

it marries 4:
system (“cls?);

cout <<? \ n \ n the division of the numbers is: “<< n1/n2 << endl << endl;

default: break;} system (“It slows down?);
return 0;}