Get Started with C++ by building a Calculator

Subscribe to my newsletter and never miss my upcoming articles

Today we're going to cover:


What is C++

C++ is one of the most popular programming languages around and for some good reasons, it's an awesome programming language. It's a language that is closely related to the "C" programming language, in fact, C++ is the next level of the "C" programming language.

Created by Bjarne Stroustrup and his team at Bell Laboratories (AT&T, USA) to help implement simulation projects in an object-oriented and efficient way.


Why C++

why_c++.png


Things you can do with C++

  • Build Game engines
  • Build interpreters
  • Build search engines
  • etc ...

Installation


Your first C++ program

Step1:

  • Start up code::block (IDE)

Screenshot from 2019-07-22 15-05-16.png

  • Click on "Create New Project", select "Console application" and click go

Screenshot from 2019-07-22 15-06-48.png

  • Select all default settings > "Next" > "C++" > "Project Name" > "Folder to create project in" > "Next" > "Finish"

Screenshot from 2019-07-22 15-08-56.png

Screenshot from 2019-07-22 15-10-57.png

Screenshot from 2019-07-22 15-13-30.png

  • On the left coner of code::block, "expand the Sources icon" > "Right Click on main.cpp" >> "Open main.cpp"

Screenshot from 2019-07-22 15-15-24.png

Screenshot from 2019-07-22 15-16-30.png

You will have your first program, the famous "Hello world!" program already on your IDE, let us go ahead an run the program. Am a very good fan of shortcuts, let us run our code by pressing the f9 on our keyboard. You should see a terminal with the string Hello world!

Screenshot from 2019-07-22 15-17-40.png

What's happening here so far

Line 1: Here we are including the iostream library, this library gives us access to read and write to the standard input/output streams. Our program will not compile unless we add it.

Line 3: using namespace std tells the compiler that symbol names defined in the std namespaces are to be brought into your program's scope. But we can omit the namespace qualifier, and write for example std::cout << "Hello world!" << endl; and our code will still build and run correctly. I love including the namespace std.

Line 5, 6, and 9: C++ only compiles and runs the codes inside of the main function.

int main() 
   {
    Our codes go here;
    return 0;
   }

We initialized a function called main and write our codes in it and return 0 at Line 8 (The return value of the main function is considered the "Exit Status" of the application, this status is like saying "The program worked fine")

Line 7: cout << "Hello world!" << endl; Here, we're printing the string Hello world! to the console. cout here is saying print out whatever we insert after it to the console, was also used it with the insertion operator which is written as << followed by double quotes, and a string inside "Hello world!" another insertion, << and end line endl telling the program that our program line ends here. Followed by a semicolon ; which stands as a closing remark for our program.
Congratulation!! You just build your first program in C++.

You made it to building your first C++ program. We're going to build a basic console calculator that collects input from the user, and perform the mathematical operations like addition, subtraction, division, and multiplication.

By building this project, you will get familiar with writing C++ codes and programming concepts like variables, logic, and computational thinking, if statements, comparison, arithmetics, inputs/outputs. etc...

Let's get started


Building a basic calculator

Step1: include the "iostream", "using namespace", initialize the main() function and return 0 the sucessfuly status quote as covered above.

 #include <iostream>

using namespace std;

int main()

    return 0;
}

We're going to start by declaring "variables" that will hold the user's input (the first number, second number, result, and the operation the wants to perform).

Variables

Variables are like our teacup, our cooking pot, handbag, or a box that holds some contents for us, and we can easily collect these elements each time we want to. In C++, it's no different, we store a lot of things into variables and can always collect what we have given to it.

Declaring variables for our program:

#include <iostream>

using namespace std;

int main()
{
    double num1, num2, result;
    char op;

    return 0;
}

Line 7, we declared a variable of type double. Double is a fundamental data type used to define numeric variables holding numbers with decimal points. The double variable can hold very large (or small) numbers. The maximum and minimum values are 17 followed by 307 zeros. The double variable is also used to hold floating-point values. A floating-point value is one like 8.7, 12.5, 10.1. In other words, it has a "point something" at the end.

We defined three variables here, "num1" to hold the first number the user will enter, the "num2" holds the second number the user enters, while the "result" the result of the operation performed on both numbers. Then, in line 8 we declared another variable called op with the char data type, the char is used to declare a single character value.

That's all the variables we'll be creating for our program. Let's ask the user to enter some information, and store them inside our variables.

cout and cin (Output and input)

We want the user to be able to perform operation on two numbers, to accomplish that, we need to ask the user to enter a number using cout (The cout object in C++ is an object of class ostream. It is used to display the output to the standard output device i.e. monitor.) and save whatever the user enters inside the declared variables, using the cin (The cin object in C++ is an object of class istream. It is used to accept the input from the standard input device i.e. keyboard.)

#include <iostream>

using namespace std;

int main()
{
    double num1, num2, result;
    char op;

    // Get the first number from the user and store it to num1
    cout << "Enter first number: ";
    cin >> num1;

    // Get the operator from the user and store it to op
    cout << "Enter operator: ";
    cin >> op;

    // Get the second number from the user and store it to num2
    cout << "Enter second number: ";
    cin >> num2;

    return 0;
}

Line 10, 14, and 18 We used what is called comments in programming, comments are used to describe the programmer's intentions, it's not executable, it reminds the programmer what he/she has done in the program.

Line 11, 15, and 19 we asked the user to enter some information, and then we used the cin object at line 12, 16, and 20 to collect whatever they entered and saved it to our variables

Logic and Computational Thinking

To fully understand Logic and Computational Thinking, check out my other article on it here

#include <iostream>

using namespace std;

int main()
{
    double num1, num2, result;
    char op;

    // Get the first number from the user and store it to num1
    cout << "Enter first number: ";
    cin >> num1;

    // Get the operator from the user and store it to op
    cout << "Enter operator: ";
    cin >> op;

    // Get the second number from the user and store it to num2
    cout << "Enter second number: ";
    cin >> num2;

    // Logic and comparsion

    if (op == '+') {
        result = num1 + num2;
    } else if (op == '-') {
        result = num1 - num2;
    } else if (op == '*') {
        result = num1 * num2;
    } else if (op == '/') {
        result = num1 / num2;
    } else {
        cout << "Syntax Error";
    }

    cout << result;

    return 0;
}

Here we're using are applying logic to our code, we're using the if, else if and else statements to check what the user entered as an operator matches with our operator, if it does, we perform computation on the inputs and assign it to the result variable. What if it doesn't then we print a string "Syntax Error" to the user. Then at line 36, we print the result of the operation to the user.

Let's see it in action

Click on the build and run icon or use f9

Screenshot from 2019-07-22 15-23-52.png

You should see your terminal in action as mine:

Screenshot from 2019-07-22 15-25-10.png

The whole code can be found here


References

Comments (1)

Quang Thắng's photo

Very easy :v