Showing posts with label How to make a calculator. Show all posts
Showing posts with label How to make a calculator. Show all posts

Function Prototype - How to Program a Complete C++ Calculator

In this tutorial in our C++ course, we will learn how to create a complete C++ calculator as well as understand what function prototypes are and what they are for.

Programming a Calculator in C++

Let's now program a very functional and useful calculator using the knowledge we learned from C++:


The sum(), sub(), mult(), and divis() functions perform the sum, subtraction, multiplication, and division operations, respectively. They all get two float data and return a float result as well.

Except for the divis() function, as it needs to test if the denominator is different than 0.
If it is, returns the division correctly.
If not, returns a message saying that it is not possible to divide by 0, which is the correct one to do.

The menu() function is responsible for showing the possible mathematical operations that it can do.
The user types a value as an option.

If this option is 0, the program exits the DO WHILE looping from the menu() and terminates the program.

If you type in any other number, we ask for the two values ​​that he will want to calculate the operation and then go to SWITCH, where we select the operation correctly.

We pass the numbers entered by the user to the respective function, she returns the result and we display it.

Finally, the options menu is displayed again, in case the user wants to do another calculation.
See how our code looks:
#include <iostream>
using namespace std;

float sum(float a, float b)
{
    return a+b;
}

float sub(float a, float b)
{
    return a-b;
}

float mult(float a, float b)
{
    return a*b;
}

float divis(float a, float b)
{
    if(b!=0)
        return a/b;
    else
        cout<<"Can't divide by 0\n";
}

void menu()
{
    int op;
    float a, b;
    do{
        cout<<"0. Exit\n";
        cout<<"1. Sum\n";
        cout<<"2. Subtraction\n";
        cout<<"3. Multiplication\n";
        cout<<"4. Division\n";
        cin >> op;

        if(op){
            cout<<"\nFirst Number: ";
            cin >> a;

            cout<<"Secont number: ";
            cin >> b;

            switch(op){
                case 1:
                    cout<<"Sum: " << sum(a,b) << endl;
                    break;
                case 2:
                    cout<<"Sub: " << sub(a,b) << endl;
                    break;
                case 3:
                    cout<<"Mult: " << mult(a,b) << endl;
                    break;
                case 4:
                    if(b)
                        cout<<"Division: " << divis(a,b) << endl;
                    else
                        divis(a,b);
                    break;
                default:
                    cout<<"Invalid option\n";
            }
        }else
            cout<<"Exiting...\n";
        cout<<endl;
    }while(op);
}

int main()
{
    menu();
    return 0;
}
In the main() function we simply invoke the function responsible for displaying the menu.
And only.

See, we use functions, mathematical operations, IF and ELSE, SWITCH and DO WHILE.
Basically, all the knowledge we have studied so far in our C++ course.

Now do an exercise.

C++ function prototypes

Take the main() function, cut it from the code, and paste it up before the functions of the math operations. Now run your code.

It should give a 'sum() was not declared' error, saying that the sum() function has not been declared.
This is because the compiler reads the code from top to bottom.

When he enters menu() and calls sum(), he doesn't know what to do, because this function has not been declared yet, it is below menu(). Therefore, it is correct to first declare the functions of mathematical operations, and only then to invoke them in the menu().

Now imagine a more complex and much larger program, how that would get complicated.
We'd have to be careful what to declare before that, and the code before the main() main function would look gigantic.

The solution to this is to declare only the function prototypes up there.

The prototype is nothing more than the function header, with the data types it receives and the correct return, followed by a semicolon, without the braces pair and function code.

Here's how our calculator code looks, using the prototypes:
#include <iostream>
using namespace std;

void menu();
float sum(float a, float b);
float sub(float a, float b);
float mult(float a, float b);
float divis(float a, float b);

int main()
{
    menu();
    return 0;
}

void menu()
{
    int op;
    float a, b;
    do{
        cout<<"0. Exit\n";
        cout<<"1. Sum\n";
        cout<<"2. Subtraction\n";
        cout<<"3. Multiplication\n";
        cout<<"4. Division\n";
        cin >> op;

        if(op){
            cout<<"\nFirst Number: ";
            cin >> a;

            cout<<"Secont number: ";
            cin >> b;

            switch(op){
                case 1:
                    cout<<"Sum: " << sum(a,b) << endl;
                    break;
                case 2:
                    cout<<"Sub: " << sub(a,b) << endl;
                    break;
                case 3:
                    cout<<"Mult: " << mult(a,b) << endl;
                    break;
                case 4:
                    if(b)
                        cout<<"Division: " << divis(a,b) << endl;
                    else
                        divis(a,b);
                    break;
                default:
                    cout<<"Invalid option\n";
            }
        }else
            cout<<"Exiting...\n";
        cout<<endl;
    }while(op);
}
float sum(float a, float b) { return a+b; } float sub(float a, float b) { return a-b; } float mult(float a, float b) { return a*b; } float divis(float a, float b) { if(b!=0) return a/b; else cout<<"Can't divide by 0\n"; }
Much more beautiful and organized, don't you think?

Incidentally, the compiler only needs to know the return, the name of the function, how many and what kind of parameters the function has, nor even their name, you can declare like this too:
void menu();
float sum(float, float);
float sub(float, float);
float mult(float, float);
float divis(float, float);
We always recommend using function prototypes, making your code more organized and less susceptible to errors and problems.

Simple C++ Calculator: How to Program

To wrap up our C++ Getting Started section, let's make a very simple, but very useful and interesting program: a calculator.

Before you see the code, try to solve it.

It must receive two user numbers, and display the operations of:
  • Sum
  • Subtraction
  • Multiplication
  • Division of the first by the second
  • Rest of the division of the first by the second
  • Percentage of the first relative to the second
  • Arithmetic average

Your program should look like:
How to make create a simple calculator in C++


Try it!

How to make a calculator in C++

See how our code looked:
#include <iostream>
using namespace std;

int main()
{
    float num1, num2;

    //Receiving data
    cout << "First number   : ";
    cin >> num1;

    cout << "Second number  : ";
    cin >> num2;

    //Displaying operations
    cout << "Sum            : " << num1 + num2 << endl;
    cout << "Subtraction    : " << num1 - num2 << endl;
    cout << "Multiplication : " << num1 * num2 << endl;
    cout << "Division       : " << num1 / num2 << endl;
    cout << "Modulus        : " << (int)num1 % (int)num2 << endl;
    cout << "Percentage     : " << 100.0*(num1/num2) << endl;
    cout << "Average        : " << (num1 + num2)/2 << endl;


    return 0;
}
Yours, how was it? Different?
Put it in the comments!

See that the only thing different was the excerpt:
(int) num1% (int) num2

This is called casting, that is, since the module operator (rest of division) only makes sense with integer values and our variables are float, we put (int) before the variables to tell C ++ that we want to handle those variables, at that time, as integers.