Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt

Use this file to discover all available pages before exploring further.

The best way to learn the MSVC toolchain is to build something real. This tutorial starts with the classic “Hello, World!” program and progressively builds it into a working console calculator that accepts arithmetic expressions, computes results, and handles invalid input gracefully. Along the way you will learn how to create classes, split code across multiple files, use the Visual Studio debugger to trace execution, and apply the Edit and Continue feature to fix bugs while the program is running.

Prerequisites

  • Visual Studio with the Desktop development with C++ workload installed. See the Installation guide if needed.
  • To use the Edit and Continue feature shown later in this tutorial, go to Tools > Options > Debugging > General and check Require source files to exactly match the original version.

Part 1 — Hello, World!

Create the Project

1

Start Visual Studio and create a new project

On the Visual Studio Start Window, click Create a new project. Set the filters to C++, Windows, Console, then select the Console App template and click Next.
2

Name and create the project

In the Configure your new project dialog, set Project name to CalculatorTutorial. Leave the location at the default and click Create.Visual Studio creates the project and opens the generated CalculatorTutorial.cpp file, which already contains a working Hello World program:
// CalculatorTutorial.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
//

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}
3

Build and run

From the Build menu, select Build Solution to compile the project. When the build succeeds, press Ctrl+F5 (Start Without Debugging) to run it.A console window appears displaying:
Hello World!
Press any key to dismiss it. Congratulations — you have confirmed that the compiler, linker, and runtime are all working correctly.

Part 2 — Building the Calculator UI

Now replace the Hello World output with the calculator’s user interface shell. Replace the entire contents of CalculatorTutorial.cpp with the following code:
// CalculatorTutorial.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
//

#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;
}
Press Ctrl+F5 to run it. The console should display the calculator prompt before exiting.
  • using namespace std; tells the compiler that names from the C++ Standard Library can be used without the std:: prefix. Instead of std::cout you can write cout.
  • cout is the standard output stream. The << operator sends values to it.
  • endl ends the current line and flushes the output buffer. For performance in production code, prefer "\n", but endl is fine in small tutorials.
  • All C++ programs start execution at main(). The return 0; signals successful completion to the operating system.

Part 3 — Adding the Calculator Class

A class is a user-defined type that bundles data and functions together. Define a Calculator class to contain all the arithmetic logic, keeping it separate from the UI code in main().

Add the Calculator class files

1

Open the Add Class dialog

From the Project menu, select Add Class. In the Class Name field, type Calculator and click OK.Visual Studio creates two new files: Calculator.h (the header/declaration) and Calculator.cpp (the implementation). Both appear under their respective filters in Solution Explorer.
2

Declare the Calculate function in Calculator.h

Open Calculator.h (double-click it in Solution Explorer) and replace its contents:
#pragma once
class Calculator
{
public:
    double Calculate(double x, char oper, double y);
};
This declares a single public member function Calculate that takes two doubles and an operator character, and returns a double result.
3

Implement Calculate in Calculator.cpp

Open Calculator.cpp and replace its contents with the full implementation:
#include "Calculator.h"

double Calculator::Calculate(double x, char oper, double y)
{
    switch(oper)
    {
        case '+':
            return x + y;
        case '-':
            return x - y;
        case '*':
            return x * y;
        case '/':
            return x / y;
        default:
            return 0.0;
    }
}
The switch statement checks which operator the user entered and executes the corresponding arithmetic. The default case returns 0.0 for any unrecognized operator.

Connect the Calculator class to main()

Update CalculatorTutorial.cpp to use the Calculator class:
// CalculatorTutorial.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
//

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

using namespace std;

int main()
{
    double x = 0.0;
    double y = 0.0;
    double result = 0.0;
    char oper = '+';

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

    Calculator c;
    while (true)
    {
        cin >> x >> oper >> y;
        result = c.Calculate(x, oper, y);
        cout << "Result " << "of " << x << oper << y << " is: " << result << endl;
    }

    return 0;
}
  • #include "Calculator.h" makes the Calculator class visible to main(). Quoted includes search the project directory first.
  • Calculator c; declares c as an instance (object) of the Calculator class. The class is the blueprint; c is the actual calculator.
  • while (true) runs the loop forever. The user must close the console window to exit — this is intentional for a REPL-style calculator.
  • cin >> x >> oper >> y; reads two numbers and an operator from standard input. cin is smart enough to parse 5+3 as x=5, oper='+', y=3.
  • c.Calculate(x, oper, y) calls the member function on the object c and returns the result.
Press Ctrl+F5 to rebuild and run. Enter 5+5 and press Enter:
Calculator Console Application

Please enter the operation to perform. Format: a+b | a-b | a*b | a/b
5+5
Result of 5+5 is: 10

Part 4 — Debugging the App

The calculator has a bug: dividing by zero produces inf rather than a useful error message. Use the debugger to observe this behavior and then fix it.

Set a conditional breakpoint

1

Set a breakpoint on the Calculate call

In CalculatorTutorial.cpp, click in the gray margin to the left of the line:
result = c.Calculate(x, oper, y);
A red dot appears — that’s your breakpoint.
2

Make the breakpoint conditional

Right-click the red dot and select Conditions. In the condition box, enter:
(y == 0) && (oper == '/')
Click Close. The breakpoint now only fires when a division by zero is attempted.
3

Run in debug mode

Press F5 to start debugging. Enter normal expressions like 5-0 — the program runs without pausing. Then enter 10/0 — execution pauses at the breakpoint.
4

Inspect variables

Look at the Autos window (auto-shown when debugging paused). You can see:
  • x = 10
  • oper = 47 (ASCII for ’/’)
  • y = 0
Press F10 (Step Over) a few times to advance through Calculate and back to main(). On the cout line, hover over result — its value is inf.

Fix the divide-by-zero bug with Edit and Continue

While still in the debug session, update CalculatorTutorial.cpp to handle division by zero explicitly. You can edit the file while the debugger is paused:
Calculator c;
while (true)
{
    cin >> x >> oper >> y;
    if (oper == '/' && y == 0)
    {
        cout << "Math error: Attempted to divide by zero!" << endl;
        continue;
    }
    else
    {
        result = c.Calculate(x, oper, y);
    }
    cout << "Result " << "of " << x << oper << y << " is: " << result << endl;
}
Press F5 to continue. Enter 10/0 again — now the error message appears instead of inf:
10/0
Math error: Attempted to divide by zero!
If you see a “code is stale” dialog while using Edit and Continue, press F5 again to reload the updated code. If the edit cannot be applied without a restart, stop debugging (Shift+F5) and relaunch (Ctrl+F5).

Part 5 — The Finished Calculator

Here is the complete, finished CalculatorTutorial.cpp with the divide-by-zero fix:
// CalculatorTutorial.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
//

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

using namespace std;

int main()
{
    double x = 0.0;
    double y = 0.0;
    double result = 0.0;
    char oper = '+';

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

    Calculator c;
    while (true)
    {
        cin >> x >> oper >> y;
        if (oper == '/' && y == 0)
        {
            cout << "Math error: Attempted to divide by zero!" << endl;
            continue;
        }
        else
        {
            result = c.Calculate(x, oper, y);
        }
        cout << "Result " << "of " << x << oper << y << " is: " << result << endl;
    }

    return 0;
}
And Calculator.h:
#pragma once
class Calculator
{
public:
    double Calculate(double x, char oper, double y);
};
And Calculator.cpp:
#include "Calculator.h"

double Calculator::Calculate(double x, char oper, double y)
{
    switch(oper)
    {
        case '+':
            return x + y;
        case '-':
            return x - y;
        case '*':
            return x * y;
        case '/':
            return x / y;
        default:
            return 0.0;
    }
}

Debugger Quick Reference

ShortcutAction
F5Start / Continue debugging
Ctrl+F5Run without debugger
Shift+F5Stop debugging
F9Toggle breakpoint on current line
F10Step Over (execute line, skip into calls)
F11Step Into (enter called functions)
Shift+F11Step Out (run until current function returns)

Next Steps

You have built, run, and debugged a multi-file C++ project in Visual Studio. From here you can explore the CMake tutorial to learn how to set up cross-platform builds, or the Linux tutorial to target Linux from Visual Studio.

Build docs developers (and LLMs) love