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.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.
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
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.
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: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: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 ofCalculatorTutorial.cpp with the following code:
Understanding the code
Understanding the code
using namespace std;tells the compiler that names from the C++ Standard Library can be used without thestd::prefix. Instead ofstd::coutyou can writecout.coutis the standard output stream. The<<operator sends values to it.endlends the current line and flushes the output buffer. For performance in production code, prefer"\n", butendlis fine in small tutorials.- All C++ programs start execution at
main(). Thereturn 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 aCalculator class to contain all the arithmetic logic, keeping it separate from the UI code in main().
Add the Calculator class files
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.Declare the Calculate function in Calculator.h
Open This declares a single public member function
Calculator.h (double-click it in Solution Explorer) and replace its contents:Calculate that takes two doubles and an operator character, and returns a double result.Connect the Calculator class to main()
UpdateCalculatorTutorial.cpp to use the Calculator class:
Understanding the code
Understanding the code
#include "Calculator.h"makes theCalculatorclass visible tomain(). Quoted includes search the project directory first.Calculator c;declarescas an instance (object) of theCalculatorclass. The class is the blueprint;cis 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.cinis smart enough to parse5+3asx=5,oper='+',y=3.c.Calculate(x, oper, y)calls the member function on the objectcand returns the result.
5+5 and press Enter:
Part 4 — Debugging the App
The calculator has a bug: dividing by zero producesinf rather than a useful error message. Use the debugger to observe this behavior and then fix it.
Set a conditional breakpoint
Set a breakpoint on the Calculate call
In A red dot appears — that’s your breakpoint.
CalculatorTutorial.cpp, click in the gray margin to the left of the line:Make the breakpoint conditional
Right-click the red dot and select Conditions. In the condition box, enter:Click Close. The breakpoint now only fires when a division by zero is attempted.
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.Fix the divide-by-zero bug with Edit and Continue
While still in the debug session, updateCalculatorTutorial.cpp to handle division by zero explicitly. You can edit the file while the debugger is paused:
10/0 again — now the error message appears instead of inf:
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, finishedCalculatorTutorial.cpp with the divide-by-zero fix:
Calculator.h:
Calculator.cpp:
Debugger Quick Reference
| Shortcut | Action |
|---|---|
| F5 | Start / Continue debugging |
| Ctrl+F5 | Run without debugger |
| Shift+F5 | Stop debugging |
| F9 | Toggle breakpoint on current line |
| F10 | Step Over (execute line, skip into calls) |
| F11 | Step Into (enter called functions) |
| Shift+F11 | Step Out (run until current function returns) |