cppappd2010 using solution folders
TRANSCRIPT
-
8/8/2019 CppAppD2010 Using Solution Folders
1/18
1
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
This appendix contains the instructions for using Microsoft Visual C++ 2010, which is a full-featured
Integrated Development Environment (IDE) for the C++ language. Visual C++ 2010 is available as a
stand-alone product, called Visual C++ 2010 Express Edition, or as part of Visual Studio 2010. At the
time of this writing, you can download a free copy of Visual C++ 2010 Express Edition at
www.microsoft.com/express/Downloads/#2010-Visual-CPP.
To start Visual Studio 2010 or Visual C++ Express Edition:
1. Click the Start button on the Windows 7 taskbar and then point toAll Programs.
2. If you are using Visual Studio 2010, clickMicrosoft Visual Studio 2010 on the All Programs
menu and then clickMicrosoft Visual Studio 2010. If the Choose Default Environment Settings
dialog box appears, clickVisual C++ Development Settings and then clickStart Visual
Studio.
If you are using Visual C++ 2010 Express Edition, clickMicrosoft Visual Studio 2010 Express
on the All Programs menu and then clickMicrosoft Visual C++ 2010 Express.
3. A copyright screen appears momentarily, and then either the Microsoft Visual Studio window or the
Microsoft Visual C++ 2010 Express window opens. ClickWindowon the menu bar, then click
Reset Window Layout, and then click theYes button.
4. If necessary, select the Close page after project load and Show page on startup check boxes
on the Start Page. When you start the Professional Edition of Visual Studio 2010, your screen will
appear similar to Figure D-1. (If you are using a different edition of Visual Studio 2010, your startup
-
8/8/2019 CppAppD2010 Using Solution Folders
2/18
2
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
screen might look slightly different than the one shown in Figure D-1.) When you start the Express
Edition of Visual C++ 2010, on the other hand, your screen will appear similar to Figure D-2.
Important note: To select a different window layout, click Tools on the menu bar, click Import and
Export Settings, select the Reset all settings radio button and then click the Next button. Select the
appropriate radio button in the Save Current Settings pane, click the Next button, click the preferred
collection of settings (such as Visual C++ Development Settings), and then click the Finish button.
Click the Close button.
Figure D-1 Microsoft Visual Studio 2010 Professional startup screen
be sure thesecheck boxesare selected
Auto Hide button
Toolboxwindow
-
8/8/2019 CppAppD2010 Using Solution Folders
3/18
3
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-2 Microsoft Visual C++ 2010 Express startup screen
Setting the Appropriate Options
Next, you will set some options so that your screen agrees with the figures and instructions in this
appendix.
To set the appropriate options:
1. If you are using Visual C++ 2010 Express Edition, clickTools on the menu bar, point to Settings,
and then clickExpert Settings (if necessary) to select it.
2. ClickTools on the menu bar and then clickOptions to open the Options dialog box.
3. Click the Projects and Solutions node. Use the information shown in Figure D-3 to select and
deselect the appropriate check boxes. (Your dialog box will look slightly different if you are using the
Express Edition.) When you are finished, click the OKbutton to close the Options dialog box.
be sure thesecheck boxes areselected
Auto Hide button
Toolboxwindow
-
8/8/2019 CppAppD2010 Using Solution Folders
4/18
4
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-3 Completed Options dialog box
Managing the Windows in the IDE
Usually, you will find it easier to work in the IDE if you either close or auto-hide the windows you are not
currently using. The easiest way to close an open window is to click the Closebutton on the windows
title bar. In most cases, the View menu provides an appropriate option for opening a closed window.
Rather than closing a window, you also can auto-hide it. You auto-hide a window using the Auto Hide
button (shown earlier in Figures D1 and D2) on the windows title bar. The Auto Hide button is a toggle
button: clicking it once activates it, and clicking it again deactivates it. The Toolbox windows in Figures
D1 and D2 are auto-hidden. In the next set of steps, you will close the windows that you will not need.
You also will practice auto-hiding and displaying the Solution Explorer window.
To manage the windows in the IDE:
1. Place your mouse pointer on the Toolbox tab. When the Toolbox window slides into view, click the
Close button on its title bar.
2. If the Document Outline tab appears in the IDE, place your mouse pointer on the tab and then click
the Closebutton on the windows title bar.
3. If the Server Explorer tab appears in the IDE, place your mouse pointer on the tab and then click the
Closebutton on the windows title bar.
select these fivecheck boxes anddeselect theothers
-
8/8/2019 CppAppD2010 Using Solution Folders
5/18
5
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
4. If necessary, close the following windows by clicking their tabs and then clicking the Close button
on their title bars: Class View, Property Manager, Team Explorer, Code Definition, Output, and
Database Explorer.
5. If the Solution Explorer window is not open in the IDE, clickViewon the menu bar and then click
Solution Explorer.
6. Click theAuto Hide(vertical pushpin) button on the Solution Explorer windows title bar to auto-
hide the window.
7. Place your mouse pointer on the Solution Explorer tab and then click theAuto Hide (horizontal
pushpin) button on the windows title bar to permanently display the window.
Creating a Console Application
Applications created in either Visual Studio 2010 or Visual C++ 2010 Express are composed of solutions,
projects, and files. A solution is a container that stores the projects and files for an entire application. A
project also is a container, but it stores files associated with that particular project. Although the idea of
solutions, projects, and files may sound confusing, the concept of placing things in containers is nothing
new to you. Think of a solution as being similar to a drawer in a filing cabinet. A project then is similar to
a file folder that you store in the drawer, and a file is similar to a document that you store in the file
folder. You can place many file folders in a filing cabinet drawer, just as you can place many projects in a
solution. You also can store many documents in a file folder, similar to the way you can store many files
in a project. Figure D-4 illustrates this analogy.
Figure D-4 Illustration of a solution, project, and file
-
8/8/2019 CppAppD2010 Using Solution Folders
6/18
6
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Although you can create many different types of C++ applications, the applications you will create in this
book are C++ console applications. A console application is a program that runs in a Command Prompt
window.
To create a C++ console application:
1. ClickFile on the menu bar, point to New, and then clickProject. The New Project dialog box
opens.
2. If necessary, clickVisual C++ in the list of Installed Templates, and then (if necessary) clickWin32
Console Application in the middle column of the dialog box.
3. If necessary, select the Create directory for solution check box.
4. Change the name in the Name box to MyFirst Project. Change the name in the Solution name box
to MyFirst Solution. Click the Browse button, which appears next to the Location box, to openthe Project Location dialog box. Locate and then click the Cpp6 folder, and then click the Select
Folder button. See Figures D-5 and D-6.
Figure D-5 Completed New Project dialog box in Visual Studio 2010
your drive lettermight be different
-
8/8/2019 CppAppD2010 Using Solution Folders
7/18
7
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-6 Completed New Project dialog box in Visual C++ 2010 Express
5. Click the OKbutton to close the New Project dialog box. The Win32 Application Wizard dialog box
appears and displays the Welcome to the Win32 Application Wizard pane. Click the Next button to
display the Application Settings pane. Verify that the Console application radio button is selected.
Select the Empty project check box. See Figure D-7.
Figure D-7 Completed Win32 Application Wizard dialog box
your drive lettermight be different
this radio buttonshould be selected
select this check box
-
8/8/2019 CppAppD2010 Using Solution Folders
8/18
8
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
6. Click the Finish button. The computer creates a solution and adds a Visual C++ project to the
solution. It also records the names of the solution and project, as well as other information pertaining
to the project, in the Solution Explorer window. See Figure D-8.
Figure D-8 Solution and project names shown in the Solution Explorer window
Adding a New Source File to an Open Project
To add a new source file to an open project:
1. Right-click the Source Files folder in the Solution Explorer window. Point toAdd and then click
New Item to open the Add New Item dialog box. ClickC++ File (.cpp) in the middle column of the
dialog box. Change the name entered in the Name box to MyFirstSourceFile. Figure D-9 shows
the completed dialog box in Visual Studio 2010. Your dialog box will look slightly different if you are
using the Express Edition.
-
8/8/2019 CppAppD2010 Using Solution Folders
9/18
9
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-9 Completed Add New Item dialog box in Visual Studio 2010
2. Click theAdd button to close the Add New Item dialog box. The computer adds an empty C++
source file to the project. It also records the files name (MyFirstSourceFile.cpp) in the Solution
Explorer window. See Figure D-10.
Figure D-10 Source file added to the project
select thistemplate
source filename
source filename
insertion point
you can use this list boxto increase/decrease thesize of the font used in thesource files window
-
8/8/2019 CppAppD2010 Using Solution Folders
10/18
10
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Adding an Existing Source File to a Project
Althoughyou dont need to add an existing source file to the current project, the following steps show
you how to accomplish that task.
To add an existing source file to an open project:
1. Right-click the Source Files folder in the Solution Explorer window. Point toAdd and then click
Existing Item to open the Add Existing Item dialog box.
2. Locate and then click the name of the fileyou want to add to the project, and then click theAdd
button.
Entering C++ Program Instructions
In the next set of steps, you will enter some C++ program instructions in the source files editor window.
Do not be concerned if you do not understand the instructions. The instructions are explained in Chapter
4.
To enter C++ program instructions in the source files editor window:
1. Auto-hide the Solution Explorer window. Doing this allows you to view more of the source files
editor window.
2. Enter the C++ program instructions shown in Figure D-11 in the MyFirstSourceFile.cpp window. Be
sure to use the exact capitalization and punctuation shown in the figure. The asterisk on the
MyFirstSourceFile.cpp tab indicates that the changes made to the file have not been saved.
-
8/8/2019 CppAppD2010 Using Solution Folders
11/18
11
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-11 C++ instructions entered in the editor window
3. Save the program. You can do so by clicking File on the menu bar and then clicking Save All. Or,
you can click the Save All button on the standard toolbar.
Important note: If you prefer to use the buttons on the standard toolbar, its best to use the Save All
button and not the Save button. This is because the Save button saves only the changes made to the item
selected in the Solution Explorer window. For example, when the source filename is selected, the Save
button saves only the changes made to the source file. Similarly, when the project name is selected, only
changes made to the files included in the project are saved. The tooltip box that appears when you rest
your mouse pointer on the Save button indicates the files that will be saved. In this case, the tooltip box
will say Save MyFirstSourceFile.cpp when the source files name is selected in the Solution Explorerwindow, Save MyFirst Project when the projects name is selected, and Save MyFirst Solution.sln
when the solutions name is selected.
asterisk
-
8/8/2019 CppAppD2010 Using Solution Folders
12/18
12
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Running a C++ Program
Before you run a C++ program, you need to save it and then build it. Building a C++ program involves
compiling the source code into object code and then invoking the linker program to link the object code
to other machine code necessary for the program to run. (You will learn about the compiler and linker
programs in Chapter 4.)
To run a C++ program:
1. Save the program (if necessary).
2. ClickBuild on the menu bar and then clickBuild Solution. (If you are using Visual C++ 2010
Express Edition and do not have the Build menu, click Tools on the menu bar, point to Settings, and
then click Expert Settings.) The compiler translates the source code into object code, and the linker
links the object code with other machine code. If your program does not contain any errors, theBuild succeeded message appears in the lower-left corner of the Output window. In addition, the
Output window displays the message Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped. See
Figure D-12. The messages indicate that the program was built (compiled and linked) successfully.
Important note: If the compiler found errors in your program, carefully compare your code with
the code shown in Figure D-11. Make any needed corrections and then repeat Steps 1 and 2.
Figure D-12 Status messages shown in the Output window
3. ClickDebug on the menu bar and then clickStart Without Debugging. The programs output
appears in a Command Prompt window, as shown in Figure D-13.
-
8/8/2019 CppAppD2010 Using Solution Folders
13/18
13
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-13 Program output appears in a Command Prompt window
Important note: Be sure to use the Start Without Debugging option rather than the Start
Debugging option. If you use the Start Debugging option, the editor will not automatically display the
Press any key to continue message in the Command Prompt window. Instead, the window will close
immediately after the program output is displayed. If you want to use the Start Debugging option,
you will need to enter the system(pause); statement above the return 0; statement in the program.
4. You can change the color of the Command Prompt windowsbackground and text to make the
display clearer. Right-click the Command Prompt windows title bar and then click
Properties. Click the Colors tab. Select the Screen Text radio button and then click theblack
square. Click the Screen Background radio button and then click either the light gray square
or thewhite square.
5. You also can change the font used to display the text in the Command Prompt window. Click the
Font tab. ClickLucida Console in the list of fonts and then select the Boldfonts check box. Click
20 in the Size list box and then click the OKbutton. See Figure D-14.
Figure D-14 Result of changing some of the Command Prompt windows properties
6. Press Enter (or any key) to close the Command Prompt window.
7. Close the Output window by clicking the Close button on its title bar.
-
8/8/2019 CppAppD2010 Using Solution Folders
14/18
14
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Displaying Line Numbers in the Editor Window
At times, its helpful to display lines numbers in the editor window.
To display lines numbers in the editor window:
1. ClickTools on the menu bar and then clickOptions to open the Options dialog box.
2. Expand the Text Editor node (if necessary) and then clickC/C++. Select the Line numbers check
box. See Figure D-15.
Figure D-15 Options dialog box
3. Click the OKbutton to close the Options dialog box. See Figure D-16.
Figure D-16 Line numbers in the editor window
select this check box
-
8/8/2019 CppAppD2010 Using Solution Folders
15/18
15
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Debugging a C++ Program
As you will learn in Chapter 4, the errors in a program are called bugs, and the process of locating and
correcting a programs bugs is called debugging. Many program bugs are caused by syntax errors. A
syntax error occurs when you break one of the programming languages rules, such as typing cut rather
than cout. In the next set of steps, you will introduce two syntax errors into the current program. You
then will learn how to locate and correct the errors.
To introduce two syntax errors in the current program and then debug the program:
1. Change cout in Line 8 to cut. Also delete the semicolon that appears at the end of Line 9. See Figure
D-17.
Figure D-17 Two errors in the MyFirstSourceFile.cpp program
2. Save the program. ClickBuild on the menu bar and then clickBuild Solution. The Error List
window indicates that the program contains 4 errors. Double-clickthe first error message in the
Error List window. See Figure D-18.
second error
first error
-
8/8/2019 CppAppD2010 Using Solution Folders
16/18
16
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-18 Errors listed in the Error List window
3. The editor displays an arrow at the location where the error was encountered in the program. In this
case, the arrow is pointing to the statement on Line 8. The error message indicates that the word
cut is undeclared. This simply means that the compiler does not recognize the word. Change cut in
Line 8 to cout.
4. Save and then build the program. The Error List window indicates that the program now contains 2
errors. Double-clickthe first error message in the Error List window. See Figure D-19.
arrow
ErrorListwindow
-
8/8/2019 CppAppD2010 Using Solution Folders
17/18
17
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Figure D-19 Remaining errors listed in the Error List window
5. The error message indicates that a semicolon is missingbefore the word return.Although the error
message and arrow refer to Line 10, its the instruction in Line 9 that caused the error. Recall that you
deleted the semicolon at the end of that line. Type ; (a semicolon) at the end of Line 9.
6. Save and then build the program. The Output window indicates that the build was successful.
7. Finally, clickDebug on the menu bar and then clickStart Without Debugging to run the
program. Press any keyto close the Command Prompt window.
8. Close the Output and Error List windows.
Printing a C++ Program
To print a programs instructions:
1. ClickFile on the menu bar and then clickPrint to open the Print dialog box. If the Include line
numbers check box is selected, line numbers will be printed even if they do not appear in the editorwindow. If the Include line numbers check box is not selected, no line numbers will appear on the
printout, even though they may appear in the editor window.
2. Select the Include line numbers checkbox. If your computer is connected to a printer, click the
OKbutton to begin printing; otherwise, click the Cancel button.
-
8/8/2019 CppAppD2010 Using Solution Folders
18/18
18
Appendix D 5/25/2010An Introduction to Programming with C++, Sixth Edition (0538466529)
Closing the Current Solution
You close a solution using the Close Solution option on the File menu. Be sure to use the Close Solution
option and not the Close option. The Close option on the File menu does not close the solution; instead,
it closes only the editor window.
To close the current solution:
1. ClickFile on the menu bar and then clickClose Solution.
2. Temporarily display the Solution Explorer window to verify that the entire solution is closed.
Opening an Existing Solution
You can use the File menu to open an existing solution. If a solution is already open in the IDE, it isclosed before another solution is opened.
To open the MyFirstProject solution:
1. Display the Solution Explorer window. ClickFile on the menu bar, point to Open, and then click
Project/Solution to open the Open Project dialog box.
2. Locate and then open the Cpp6\MyFirst Solution folder. ClickMyFirstSolution.sln in the list of
filenames and then click the Open button. If necessary, double-click the name of the .cpp file in
the Solution Explorer window to open the file in the editor.
You have completed Appendix D.