How To Make a Simple Calculator with C++ - Just a test blog
That's all folks!

If you're reading this, you must know how to program in C++, If not read my C++ - Introduction Here first and then continue on with this tutorial.

The purpose of this tutorial just to keep you in practice, if you're learning C++ then this could be a good practice. Calculations in C++ are quite like many other languages, you could simply add/subtract/multiply or divide by their defined symbols ( + - * / ).

Making the Program:

To get started, first create a simple C++ structure:

#include <iostream>

using namespace std;

int main()
{
    return 0;
}

Now create 3 variables in the 'int main()' function area:

#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum;


    return 0;
}

And add 2 'cout' and 'cin' properties. Point 'cin' to 'a' and the second 'cin' to 'b' variable. When user puts in anything in any of those 'cin' properties, the variable's value will become what the user has put in.

#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum;

    cout << "Enter a number: ";
    cin >> a;

    cout << "Enter another number: ";
    cin >> b;


    return 0;
}

Now, using our third variable 'sum' we are going to add the value of  'a' and 'b'.

#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum;

    cout << "Enter a number: ";
    cin >> a;

    cout << "Enter another number: ";
    cin >> b;

    sum = a + b;

    return 0;
}

And to display the total, we will 'cout' the value of  'sum' and end the program there.

#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum;

    cout << "Enter a number: ";
    cin >> a;

    cout << "Enter another number: ";
    cin >> b;

    sum = a + b;
    cout << "Total: " << sum << endl;

    return 0;
}

And.. your calculator is 'made'. (I've been watching a lot of Italian movies)

Testing the Program:

when you compile and run, It'll ask you to enter a number, enter one:


Enter another number:


And it will count:


Yay! It works!

I recommend you to play around with the values, maybe add a subtract option or divide the total value. You could do all that very easily but you just have to stick around for a while in your compiler program.

If you have any question or anything, Please let me know down below.

SHARE ON:

FACEBOOK TWITTER GOOGLE+