How To Create a Calculator Script in C++

The programming language C++ or C plus plus is the enhanced version of the C language program. Originally named C with classes then later changed to C++, it is considered a mid level programming language and is widely used by programmers all over the world because of its ease of use for specific and general purpose applications. C++ is constantly used in application software, systems software, and video game software among others. Simple to complicated programs can be made with this program. We'll take a look at how a simple calculator with simple operations can be created with the C++ program.

1. Learn the C++ programming language.

You must know how to program using C++. There are books that can get you started in understanding how to program with C++. You can find some books online or even websites that offer free tutorials.

2. Write the code in your text editor.

In your text editor or whatever tool you use to write your C++ codes in, you may have this code already prepared for you:

#include <cstdlib>

#include <iostream>

using namespace std;

int main (int argc, char *argv[])

{

    system("PAUSE");

    return EXIT_SUCCESS;

}

We'll be writing the code in between int main (int argc, char *argv[])

{

and

system("PAUSE");

return EXIT_SUCCESS;

}  

But first we need to change a few things and set the title and the colors for the font. In between the parenthesis after int main () type in the word void. Then set the system codes and other variables so the first part of the code will look like this:

#include <cstdlib>

#include <iostream>

using namespace std;

int main(void)

{

            system("TITLE My Calculator");

            system("COLOR 6");

            char cChar;

            double dfirstnumber;

            double dsecondnumber;

            char cDoagain;

We used the number 6 in the color setting which gives the font a yellow hue so we can clearly see it against a black background.

The second part contains the set of instructions for the calculator's user and set the case for the numbers and operators to accomplish the mathematical function. So the second part will be written like this:

do

{

            system("CLS");

            cout << "Enter a number"

                << endl;

            cin >> dfirstnumber;

            cout << "Enter an operator"

                << " (+,-,*,/)" <<endl;

            cin >> cChar;

            cout << "enter a second number"

                << endl;

            cin >> dsecondnumber;

            switch (cChar)

            {

            case '+':

                cout << "Total: " << dfirstnumber << " + " <<

                        dsecondnumber << " = " << (dfirstnumber + dsecondnumber)

                        << endl;

                break;                     

    case '-':

        cout << "Total: " << dfirstnumber << " - " <<

                        dsecondnumber << " = " << (dfirstnumber - dsecondnumber)

                        << endl;

            break;

            case '*':

                cout << "Total: " << dfirstnumber << " * " <<

                        dsecondnumber << " = " << (dfirstnumber * dsecondnumber)

                        << endl;

            break;

            case '/':

                cout << "Total: " << dfirstnumber << " / " <<

                        dsecondnumber << " = " << (dfirstnumber / dsecondnumber)

                        << endl;

            break;

After the mathematical function has been done, the third and last part will give the user another set of instructions to either start again or finish and close the calculator. The last part will be written like this:

}

        cout << "start again? (y or n)" << endl;

            cin >> cDoagain;

}while (cDoagain == 'y');

system("PAUSE");

return 0;

}

Now you can compile and run or execute your calculator. Test for bugs if any and make the necessary corrections.

 

Share this article!

Follow us!

Find more helpful articles: