You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/get-started/2019/tutorial-console-cpp.md
+28-25Lines changed: 28 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,32 +19,35 @@ The usual starting point for a C++ programmer is a "Hello, world!" application t
19
19
20
20
Visual Studio uses *projects* to organize the code for an app, and *solutions* to organize your projects. A project contains all the options, configurations, and rules used to build your apps. It also manages the relationship between all the project's files and any external files. To create your app, first, you'll create a new project and solution.
21
21
22
-
1. On the menubar in Visual Studio, choose **File** > **New** > **Project**. The **New Project** window opens.
22
+
1. On the menubar in Visual Studio, choose **File** > **New** > **Project**. The **Create a new project** window opens.
23
23
24
-
2. On the left sidebar, make sure **Visual C++** is selected. In the center, choose **Windows Console Application**.
24
+

25
25
26
-
3. In the **Name** edit box at the bottom, name the new project *CalculatorTutorial*, then choose **OK**.
26
+
2. In the list of project templates, choose **Console App**, then choose **Next**.
27
27
28
-

28
+

29
+
30
+
3. In the **Configure your new project** dialog box, select the **Project name** edit box, name your new project *CalculatorTutorial*, then choose **Create**.
31
+
32
+

29
33
30
34
An empty C++ Windows console application gets created. Console applications use a Windows console window to display output and accept user input. In Visual Studio, an editor window opens and shows the generated code:
31
35
32
36
```cpp
33
37
// CalculatorTutorial.cpp : This file contains the 'main' function. Program execution begins and ends there.
34
38
//
35
39
36
-
#include"pch.h"
37
40
#include<iostream>
38
41
39
42
intmain()
40
43
{
41
-
std::cout << "Hello World!\n";
44
+
std::cout << "Hello World!\n";
42
45
}
43
46
44
47
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
45
48
// Debug program: F5 or Debug > Start Debugging menu
46
49
47
-
// Tips for Getting Started:
50
+
// Tips for Getting Started:
48
51
// 1. Use the Solution Explorer window to add/manage files
49
52
// 2. Use the Team Explorer window to connect to source control
50
53
// 3. Use the Output window to see build output and other messages
@@ -55,15 +58,15 @@ Visual Studio uses *projects* to organize the code for an app, and *solutions* t
55
58
56
59
## Verify that your new app builds and runs
57
60
58
-
The template for a new windows console application creates a simple C++ "Hello World" app. At this point, you can see how Visual Studio builds and runs the apps you create right from the IDE.
61
+
The template for a new Windows console application creates a simple C++ "Hello World" app. At this point, you can see how Visual Studio builds and runs the apps you create right from the IDE.
59
62
60
63
1. To build your project, choose **Build Solution** from the **Build** menu. The **Output** window shows the results of the build process.
61
64
62
-

65
+

63
66
64
67
1. To run the code, on the menu bar, choose **Debug**, **Start without debugging**.
65
68
66
-

69
+

67
70
68
71
A console window opens and then runs your app. When you start a console app in Visual Studio, it runs your code, then prints "Press any key to continue . . ." to give you a chance to see the output. Congratulations! You've created your first "Hello, world!" console app in Visual Studio!
69
72
@@ -118,7 +121,7 @@ Now let's turn the code in this template into a calculator app.
118
121
119
122
1. To run the application, press **Ctrl+F5** or go to the **Debug** menu and choose **Start Without Debugging**. If you get a pop-up that says **This project is out of date**, you may select **Do not show this dialog again**, and then choose **Yes** to build your application. You should see a console window appear that displays the text specified in the code.
120
123
121
-

124
+

122
125
123
126
1. Close the console window when you're done.
124
127
@@ -130,11 +133,11 @@ It's time to add some math logic.
130
133
131
134
1. Go to the **Project** menu and choose **Add Class**. In the **Class Name** edit box, enter *Calculator*. Choose **OK**. Two new files get added to your project. To save all your changed files at once, press **Ctrl+Shift+S**. It's a keyboard shortcut for **File** > **Save All**. There's also a toolbar button for **Save All**, an icon of two floppy disks, found beside the **Save** button. In general, it's good practice to do **Save All** frequently, so you don't miss any files when you save.
132
135
133
-

136
+

134
137
135
138
A class is like a blueprint for an object that does something. In this case, we define a calculator and how it should work. The **Add Class** wizard you used above created .h and .cpp files that have the same name as the class. You can see a full list of your project files in the **Solution Explorer** window, visible on the side of the IDE. If the window isn't visible, you can open it from the menu bar: choose **View** > **Solution Explorer**.
You should now have three tabs open in the editor: *CalculatorTutorial.cpp*, *Calculator.h*, and *Calculator.cpp*. If you accidentally close one of them, you can reopen it by double-clicking it in the **Solution Explorer** window.
140
143
@@ -158,7 +161,7 @@ It's time to add some math logic.
158
161
159
162
1. You'll see a green squiggle appear under `Calculate`. It's because we haven't defined the `Calculate` function in the .cpp file. Hover over the word, click the lightbulb that pops up, and choose **Create definition of 'Calculate' in Calculator.cpp**. A pop-up appears that gives you a peek of the code change that was made in the other file. The code was added to *Calculator.cpp*.
160
163
161
-

164
+

162
165
163
166
Currently, it just returns 0.0. Let's change that. Press **Esc** to close the pop-up.
164
167
@@ -250,7 +253,7 @@ Now it's time to test the program again to make sure everything works properly.
250
253
251
254
1. Enter `5 + 5`, and press **Enter**. Verify that the result is 10.
252
255
253
-

256
+

254
257
255
258
## Debug the app
256
259
@@ -260,33 +263,33 @@ Since the user is free to type anything into the console window, let's make sure
260
263
261
264
1. Set a breakpoint on the `result = c.Calculate(x, oper, y);` line, just after the user was asked for input. To set the breakpoint, click next to the line in the gray vertical bar along the left edge of the editor window. A red dot appears.
262
265
263
-

266
+

264
267
265
268
Now when we debug the program, it always pauses execution at that line. We already have a rough idea that the program works for simple cases. Since we don't want to pause execution every time, let's make the breakpoint conditional.
266
269
267
270
1. Right-click the red dot that represents the breakpoint, and choose **Conditions**. In the edit box for the condition, enter `(y == 0) && (oper == '/')`. Choose the **Close** button when you're done. The condition is saved automatically.
268
271
269
-

272
+

270
273
271
274
Now we pause execution at the breakpoint specifically if a division by 0 is attempted.
272
275
273
276
1. To debug the program, press **F5**, or choose the **Local Windows Debugger** toolbar button that has the green arrow icon. In your console app, if you enter something like "5 - 0", the program behaves normally and keeps running. However, if you type "10 / 0", it pauses at the breakpoint. You can even put any number of spaces between the operator and numbers; `cin` is smart enough to parse the input appropriately.
274
277
275
-

278
+

276
279
277
280
### Useful windows in the debugger
278
281
279
282
Whenever you debug your code, you may notice that some new windows appear. These windows can assist your debugging experience. Take a look at the **Autos** window. The **Autos** window shows you the current values of variables used at least three lines before and up to the current line.
To see all of the variables from that function, switch to the **Locals** window. You can actually modify the values of these variables while debugging, to see what effect they would have on the program. In this case, we'll leave them alone.
You can also just hover over variables in the code itself to see their current values where the execution is currently paused. Make sure the editor window is in focus by clicking on it first.
288
291
289
-

292
+

290
293
291
294
### To continue debugging
292
295
@@ -296,11 +299,11 @@ You can also just hover over variables in the code itself to see their current v
296
299
297
300
1. Continue using **F10** to **Step Over** each line until you get back to the `main()` function in the other file, and stop on the `cout` line.
298
301
299
-

302
+

300
303
301
304
1. It looks like the program is doing what is expected: it takes the first number, and divides it by the second. On the `cout` line, hover over the `result` variable or take a look at `result` in the **Autos** window. You'll see its value is listed as "inf", which doesn't look right, so let's fix it. The `cout` line just outputs whatever value is stored in `result`, so when you step one more line forward using **F10**, the console window displays:
302
305
303
-

306
+

304
307
305
308
This result happens because division by zero is undefined, so the program doesn't have a numerical answer to the requested operation.
306
309
@@ -352,7 +355,7 @@ Let's handle division by zero more gracefully, so a user can understand the prob
352
355
353
356
1. Now press **F5** once. Program execution continues all the way until it has to pause to ask for user input. Enter `10 / 0` again. Now, a more helpful message is printed. The user is asked for more input, and the program continues executing normally.
354
357
355
-

358
+

356
359
357
360
> ![Note]
358
361
> When you edit code while in debugging mode, there is a risk of code becoming stale. This happens when the debugger is still running your old code, and has not yet updated it with your changes. The debugger pops up a dialog to inform you when this happens. Sometimes, you may need to press **F5** to refresh the code being executed. In particular, if you make a change inside a function while the point of execution is inside that function, you'll need to step out of the function, then back into it again to get the updated code. If that doesn't work for some reason and you see an error message, you can stop debugging by clicking on the red square in the toolbar under the menus at the top of the IDE, then start debugging again by entering **F5** or by choosing the green "play" arrow beside the stop button on the toolbar.
@@ -372,7 +375,7 @@ Let's handle division by zero more gracefully, so a user can understand the prob
372
375
373
376
Congratulations! You've completed the code for the calculator app, and built and debugged it in Visual Studio.
0 commit comments