Creating a C++ Calculator (Console App)

C++ Calculator

Hello friends, in this post I will explain how you can create a calculator with C++. We will make a calculator that can perform addition, subtraction, division, and exponentiation with the numbers entered by the user. Let's not waste any more time and get straight to our code.

#include <iostream>
#include <math.h>

These codes are the libraries that ensure our codes work by default. The important library here is the "math.h" library, which allows us to perform mathematical operations.

using namespace std;
float toplama(float x, float y) 
{
return x + y;
}
float cikarma(float x, float y)
{
return x - y;
}
float carpma(float x, float y)
{
return x * y;
}
float bolme(float x, float y) 
{
return x / y;
}
float ussu(float x, float y)
{
return pow(x, y);
}

We created a function for each mathematical operation we want to perform so that we can operate more easily. This avoids duplicated code.
The "using namespace std;" at the top saves us from having to write std before every command.

int main()
{
setlocale(LC_ALL, "turkish");
float sayi1, sayi2, sonuc;
int islemkontrol;
char islem;

cout << "İşlemler (+)toplama (-)çıkartma (*)çarpma (/)bölme (^)üssü"<<endl;

islemsifirlama:

cin >> sayi1 >> islem >> sayi2;

islemdevam:

switch (islem)
{
case '+':
sonuc = toplama(sayi1, sayi2);
cout << sonuc<<endl;
break;
case '-':
sonuc = cikarma(sayi1,sayi2);
cout << sonuc<<endl;
break;
case '*':
sonuc = carpma(sayi1,sayi2);
cout << sonuc<<endl;
break;
case '/':
sonuc = bolme(sayi1,sayi2);
cout << sonuc<<endl;
break;
case '^':
sonuc = ussu(sayi1, sayi2);
cout << sonuc << endl;
break;
default:
cout << "You entered an operation outside of my capabilities";
break;
}

kontroltekrari:

cout << "Enter 0 to end the operation, 1 to continue, 2 to reset operations"<<endl;
cin >> islemkontrol;


switch (islemkontrol)
{
case 0:
break;
case 1:
sayi1 = sonuc;
cout << sayi1<<" ";
cin >> islem >> sayi2;
goto islemdevam;
case 2:
goto islemsifirlama;
default:
cout << "Sorry, you entered invalid data!!!"<<endl;
goto kontroltekrari;
}

}

Here, in the main function, we first wrote "setlocale(LC_ALL, \"turkish\");" to solve the Turkish character error, and now we can use Turkish characters smoothly in our program.

After that, we defined our variables of type float, int, and char, then asked the user what operation they want to do.

In the islemsifirlama: part, we got the numbers entered by the user.
With islemdevam:, we created a "switch case" loop to take the numbers and operation entered by the user and show the result to the user, and depending on the operation requested, we calculated the result in the "switch" structure and presented it to the user.

In the kontroltekrari: section, we asked the user whether they want to continue or what they want to do.
We passed the result given by the user into a new "switch" structure, and with the "goto" command, we directed the user to the relevant section depending on what they wanted to do.

Now let’s take a look at the complete code as a whole.

#include <iostream>
#include <math.h>
/* Osman ERDEM C++ Calculator */
using namespace std;
float toplama(float x, float y) 
{
	return x + y;
}
float cikarma(float x, float y)
{
	return x - y;
}
float carpma(float x, float y)
{
	return x * y;
}
float bolme(float x, float y) 
{
	return x / y;
}
float ussu(float x, float y)
{
	return pow(x, y);
}


int main()
{
	setlocale(LC_ALL, "turkish");
	float sayi1, sayi2, sonuc;
	int islemkontrol;
	char islem;

	cout << "İşlemler (+)toplama (-)çıkartma (*)çarpma (/)bölme (^)üssü"<<endl;

islemsifirlama:

	cin >> sayi1 >> islem >> sayi2;

islemdevam:

	switch (islem)
	{
	case '+':
		sonuc = toplama(sayi1, sayi2);
		cout << sonuc<<endl;
		break;
	case '-':
		sonuc = cikarma(sayi1,sayi2);
		cout << sonuc<<endl;
		break;
	case '*':
		sonuc = carpma(sayi1,sayi2);
		cout << sonuc<<endl;
		break;
	case '/':
		sonuc = bolme(sayi1,sayi2);
		cout << sonuc<<endl;
		break;
	case '^':
		sonuc = ussu(sayi1, sayi2);
		cout << sonuc << endl;
		break;
	default:
		cout << "You entered an operation outside of my capabilities";
		break;
	}

kontroltekrari:

	cout << "Enter 0 to end the operation, 1 to continue, 2 to reset operations"<<endl;
	cin >> islemkontrol;


	switch (islemkontrol)
	{
	case 0:
		break;
	case 1:
		sayi1 = sonuc;
		cout << sayi1<<" ";
		cin >> islem >> sayi2;
		goto islemdevam;
	case 2:
		goto islemsifirlama;
	default:
		cout << "Sorry, you entered invalid data!!!"<<endl;
		goto kontroltekrari;
	}

}