Build a calculator with C++

Create a C++ Application

1. Go to File → New → Project in Visual Studio. The New Project window will open.

2. On the left sidebar, make sure Visual C++ is selected. Click on Windows Console Application.

3. At the bottom, name the new project “CalculatorTutorial”, then click OK.
Create your project This will create an empty C++ Windows Console Application. Console applications execute instructions and use a Windows terminal window to display output and accept user input.

4. In your CalculatorTutorial.cpp file, add the following code. Code that isn’t highlighted below should already be present in the file.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
    cout << "Calculator Console Application" << endl << endl;
    cout << "Please enter the operation to perform. Format: a+b | a-b | a*b | a/b" 
        << endl;
    return 0;

Understanding the code

  • The #include statements allow you to reference code located in other files. Sometimes, you will see a filename surrounded by angle brackets (<>); other times, it’s surrounded by quotes (“”). In general, angle brackets are used when referencing the C++ Standard Library, while quotes are used for other files.
  • The #include "stdafx.h" line references something known as a precompiled header. These are often used by professional programmers to improve compilation times, but they are beyond the scope of this tutorial.
  • The using namespace std line tells the compiler to expect stuff from the C++ Standard Library to be used in this file. Without this line, each keyword from the library would have to be preceded with a std::, to denote its scope. For instance, without that line, each reference to cout would have to be written as std::cout. The using statement is added to make the code look more clean.
  • The cout keyword is used to print to standard output in C++. The << operator essentially tells the compiler to send whatever is to the right of it to standard output.
  • The endl keyword is like the Enter key - it ends the line and moves the cursor to the next line. It is a better practice to put a \n inside the string (contained by “”) to do the same thing, as endl always flushes the buffer and can hurt the performance of the program, but since this is a very small app, endl is used instead for better readability.
  • All C++ statements must end with semicolons and all C++ applications must contain a main() function. This function is what the program runs at the start. All code must be accessible from main() in order to be used.

5. Save the file with Ctrl + S, or by clicking the Save icon near the top of the IDE, under the menu bar.

6. To run the application, press Ctrl + F5 or go to the Debug menu and choose Start Without Debugging. If you get a “This project is out of date” pop-up, feel free to click “Do not show this dialog again”, and click Yes. You should see a terminal window pop up with the text specified in the code. First launch 7. Close the terminal window when you are done.

I ran into an issue Continue tutorial