1.10 Test-Driving a C++ Application

In this section, you’ll compile, run and interact with your first C++ application—an entertaining guess-the-number game, which picks a number from 1 to 1000 and prompts you to guess it. If your guess is correct, the game ends. If your guess is not correct, the application indicates whether your guess is higher or lower than the correct number. There is no limit on the number of guesses you can make. [Note: For this test drive only, we’ve modified this application from the exercise you’ll be asked to create in Chapter 6, Functions and an Introduction to Recursion. Normally this application randomly selects the correct answer as you execute the program. The modified application uses the same correct answer every time the program executes (though this may vary by compiler), so you can use the same guesses we use in this section and see the same results as we walk you through interacting with your first C++ application.]

We’ll demonstrate running a C++ application using

The application runs similarly on all three platforms. You need to read only the section that corresponds to your operating system and compiler. Many development environments are available in which you can compile, build and run C++ applications—CodeLite, Clion, NetBeans and Eclipse are just a few. Consult your instructor or the online documentation for information on your specific development environment.

In the following steps, you’ll run the application and enter various numbers to guess the correct number. The elements and functionality that you see in this application are typical of those you’ll learn to program in this book. We use fonts to distinguish between features you see on the screen and elements that are not directly related to the screen. We emphasize screen features like titles and menus (e.g., the File menu) in a semibold sans-serif bold font and emphasize filenames, text displayed by an application and values you should enter into an application (e.g., GuessNumber or 500) in a sans-serif font.

1.10.1 Compiling and Running an Application in Visual Studio 2015 for Windows

In this section, you’ll run a C++ program on Windows using Microsoft Visual Studio 2015 Community Edition. We assume that you’ve already read the Before You Begin section for instructions on installing the IDE and downloading the book’s code examples.

There are several versions of Visual Studio available—on some versions, the options, menus and instructions we present might differ slightly. From this point forward, we’ll refer to Visual Studio 2015 Community Edition simply as “Visual Studio” or “the IDE.”

Step 1: Checking Your Setup

It’s important to read this book’s Before You Begin section to make sure that you’ve installed Visual Studio and copied the book’s examples to your hard drive correctly.

Step 2: Launching Visual Studio

Open Visual Studio from the Start menu. The IDE displays the Start Page (Fig. 1.12), which provides links for creating new programs, opening existing programs and learning about the IDE and various programming topics. Close this window for now by clicking the X in its tab—you can access this window any time by selecting View > Start Page. We use the > character to indicate selecting a menu item from a menu. For example, the notation File > Open indicates that you should select the Open menu item from the File menu.

Fig. 1.12 Visual Studio 2015 Community Edition window showing the Start Page.

Step 3: Creating a Project

A project is a group of related files, such as the C++ source-code files that compose an application. Visual Studio organizes applications into projects and solutions, which contain one or more projects. Multiple-project solutions are used to create large-scale applications. Each application in this book will be a solution containing a single project.

The Visual Studio projects we created for this book’s examples are Win32 Console Application projects that you’ll execute from the IDE. To create a project:

  1. Select File > New > Project….

  2. At the New Project dialog’s left side, select the category Installed > Templates > Visual C++ > Win32 (Fig. 1.13).

  3. In the New Project dialog’s middle section, select Win32 Console Application.

  4. Provide a name for your project in the Name field—we specified Guess Number—then click OK to display the Win32 Application Wizard window, then click Next > to display the Application Settings step.

    Fig. 1.13 Visual Studio 2015 Community Edition New Project dialog.

  5. Configure the settings as shown in Fig. 1.14 to create a solution containing an empty project, then click Finish.

    Fig. 1.14 Win32 Application Wizard window’s Application Settings step.

At this point, the IDE creates your project and places its folder in


C:UsersYourUserAccountDocumentsVisual Studio 2015Projects

then opens the window in Fig. 1.15. This window displays editors as tabbed windows (one for each file) when you’re editing code. Also displayed is the Solution Explorer in which you can view and manage your application’s files. In this book’s examples, you’ll typically place each program’s code files in the Source Files folder. If the Solution Explorer is not displayed, you can display it by selecting View > Solution Explorer.

Fig. 1.15 Visual Studio window after creating the Guess Number project.

Step 4: Adding the GuessNumber.cpp File into the Project

Next, you’ll add GuessNumber.cpp to the project you created in Step 3. In Windows Explorer (Windows 7) or File Explorer (Windows 8 and 10), open the ch01 folder in the book’s examples folder, then drag GuessNumber.cpp onto the Source Files folder in the Solution Explorer.10

Step 5: Compiling and Running the Project

To compile and run the project so you can test-drive the application, select Debug > Start without debugging or simply type Ctrl + F5. If the program compiles correctly, the IDE opens a Command Prompt window and executes the program (Fig. 1.16)—we changed the Command Prompt’s color scheme to make the screen captures more readable. The application displays "Please type your first guess.", then displays a question mark (?) as a prompt on the next line.

Fig. 1.16 Command Prompt showing the running program.

Step 6: Entering Your First Guess

Type 500 and press Enter. The application displays "Too high. Try again." (Fig. 1.17), meaning that the value you entered is greater than the number the application chose as the correct guess.

Fig. 1.17 Entering an initial guess and receiving feedback.

Step 7: Entering Another Guess

At the next prompt, enter 250 (Fig. 1.18). The application displays "Too high. Try again.", because the value you entered once again is greater than the correct guess.

Fig. 1.18 Entering a second guess and receiving feedback.

Step 8: Entering Additional Guesses

Continue to play the game (Fig. 1.19) by entering values until you guess the correct number. When you guess correctly, the application displays "Excellent! You guessed the number."

Step 9: Playing the Game Again or Exiting the Application

After you guess the correct number, the application asks if you’d like to play another game. At the "Would you like to play again (y or n)?" prompt, entering the one character y

Fig. 1.19 Entering additional guesses and guessing the correct number.

causes the application to choose a new number and displays the message "Please type your first guess." followed by a question-mark prompt so you can make your first guess in the new game. Entering the character n terminates the application. Each time you execute this application from the beginning (Step 5), it will choose the same numbers for you to guess.

1.10.2 Compiling and Running Using GNU C++ on Linux

For this test drive, we assume that you read the Before You Begin section and that you placed the downloaded examples in your home directory on your Linux system. Please see your instructor if you have any questions regarding copying the files to your home directory. In this section’s figures, we use bold text to highlight the text that you type. The prompt in the shell on our system uses the tilde (~) character to represent the home directory, and each prompt ends with the dollar sign ($) character. The prompt will vary among Linux systems.

Step 1: Locating the Completed Application

From a Linux shell, use the command cd to change to the completed application directory (Fig. 1.20) by typing


cd examples/ch01

then pressing Enter.

Fig. 1.20 Changing to the GuessNumber application’s directory.

Alternate View

~$ cd examples/ch01
~/examples/ch01$

Step 2: Compiling the Application

Before running the application, you must first compile it (Fig. 1.21) by typing


g++ -std=c++14 GuessNumber.cpp -o GuessNumber

This command compiles the application for C++14 (the current C++ version) and produces an executable file called GuessNumber.

Fig. 1.21 Compiling the GuessNumber application using the g++ command.

Alternate View

~/examples/ch01$ g++ -std=c++14 GuessNumber.cpp -o GuessNumber
~/examples/ch01$

Step 3: Running the Application

To run the executable file GuessNumber, type ./GuessNumber at the next prompt, then press Enter (Fig. 1.22). The ./ tells Linux to run from the current directory and is required to indicate that GuessNumber is an executable file.

Fig. 1.22 Running the GuessNumber application.

Alternate View

~/examples/ch01$ ./GuessNumber
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
?

Step 4: Entering Your First Guess

The application displays "Please type your first guess.", then displays a question mark (?) as a prompt on the next line (Fig. 1.22). At the prompt, enter 500 (Fig. 1.23). [Note that the outputs may vary based on the compiler you’re using.]

Fig. 1.23 Entering an initial guess.

Alternate View

~/examples/ch01$ ./GuessNumber
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
? 500
Too high. Try again.
?

Step 5: Entering Another Guess

The application displays "Too high. Try again.", meaning that the value you entered is greater than the number the application chose as the correct guess (Fig. 1.23). At the next prompt, enter 250 (Fig. 1.24). This time the application displays "Too low. Try again.", because the value you entered is less than the correct guess.

Fig. 1.24 Entering a second guess and receiving feedback.

Alternate View

~/examples/ch01$ ./GuessNumber
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
? 500
Too high. Try again.
? 250
Too low. Try again.
?

Step 6: Entering Additional Guesses

Continue to play the game (Fig. 1.25) by entering values until you guess the correct number. When you guess correctly, the application displays "Excellent! You guessed the number."

Fig. 1.25 Entering additional guesses and guessing the correct number.

Alternate View

Too low. Try again.
? 375
Too low. Try again.
? 437
Too high. Try again.
? 406
Too high. Try again.
? 391
Too high. Try again.
? 383
Too low. Try again.
? 387
Too high. Try again.
? 385
Too high. Try again.
? 384
Excellent! You guessed the number.
Would you like to play again (y or n)?

Step 7: Playing the Game Again or Exiting the Application

After you guess the correct number, the application asks if you’d like to play another game. At the "Would you like to play again (y or n)?" prompt, entering the one character y causes the application to choose a new number and displays the message "Please type your first guess." followed by a question-mark prompt so you can make your first guess in the new game. Entering the character n ends the application, returns you to the shell and awaits your next command. Each time you execute this application from the beginning (i.e., Step 3), it will choose the same numbers for you to guess.

1.10.3 Compiling and Running with Xcode on Mac OS X

In this section, we present how to run a C++ program on a Mac OS X using Apple’s Xcode IDE.

Step 1: Checking Your Setup

It’s important to read this book’s Before You Begin section to make sure that you’ve installed Apple’s Xcode IDE and copied the book’s examples to your hard drive correctly.

Step 2: Launching Xcode

Open a Finder window, select Applications and double click the Xcode icon (). If this is your first time running Xcode, the Welcome to Xcode window will appear (Fig. 1.26). Close this window for now—you can access it any time by selecting Window > Welcome to Xcode. We use the > character to indicate selecting a menu item from a menu. For example, the notation File > Open… indicates that you should select the Open… menu item from the File menu.

Fig. 1.26 Welcome to Xcode window.

Step 3: Creating a Project

A project is a group of related files, such as the C++ source-code files that compose an application. The Xcode projects we created for this book’s examples are OS X Command Line Tool projects that you’ll execute directly in the IDE. To create a project:

  1. Select File > New > Project….

  2. In the OS X subcategory Application, select Command Line Tool and click Next.

  3. Provide a name for your project in the Product Name field—we specified Guess Number.

  4. Ensure that the selected Language is C++ and click Next.

  5. Specify where you want to store your project, then click Create. (See the Before You Begin section for information on configuring a project to use C++14.)

Figure 1.27 shows the workspace window that appears after you create the project. By default, Xcode creates a main.cpp source-code file containing a simple program that displays "Hello, World!". The window is divided into four main areas below the toolbar: the Navigator area, Editor area and Utilities area are displayed initially. We’ll explain momentarily how to display the Debug area in which you’ll run and interact with the program.

Fig. 1.27 Sample Xcode C++ project with main.cpp selected.

At the left of the workspace window is the Navigator area, which has icons at its top for the navigators that can be displayed there. For this book, you’ll primarily work with

  • Project ()—Shows all the files and folders in your project.

  • Issue ()—Shows you warnings and errors generated by the compiler.

You choose which navigator to display by clicking the corresponding button above the Navigator area of the window.

To the right of the Navigator area is the Editor area for editing source code. This area is always displayed in your workspace window. When you select a file in the Project navigator, the file’s contents are displayed in the Editor area. At the right side of the workspace window is the Utilities area, which you will not use in this book. The Debug area, when displayed, appears below the Editor area.

The toolbar contains options for executing a program (Fig. 1.28(a)), a display area (Fig. 1.28(b)) to shows the progress of tasks executing in Xcode (such as the compilation status) and buttons (Fig. 1.28(c)) for hiding and showing areas in the workspace window.

Fig. 1.28 Xcode 7 toolbar.

Step 4: Deleting the main.cpp File from the Project

You won’t use main.cpp in this test-drive, so you should delete the file. In the Project navigator, right click the main.cpp file and select Delete. In the dialog that appears, select Move to Trash to delete the file from your system—the file will not be removed completely until you empty your trash.

Step 5: Adding the GuessNumber.cpp File into the Project

Next, you’ll add GuessNumber.cpp to the project you created in Step 3. In a Finder window, open the ch01 folder in the book’s examples folder, then drag GuessNumber.cpp onto the Guess Number folder in the Project navigator. In the dialog that appears, ensure that Copy items if needed is checked, then click Finish.11

Step 6: Compiling and Running the Project

To compile and run the project so you can test-drive the application, simply click the run () button at the left side of Xcode’s toolbar. If the program compiles correctly, Xcode opens the Debug area (at the bottom of the Editor area) and executes the program in the right half of the Debug area (Fig. 1.29). The application displays "Please type your first guess.", then displays a question mark (?) as a prompt on the next line.

Fig. 1.29 Debug area showing the running program.

Step 7: Entering Your First Guess

Click in the Debug area, then type 500 and press Return. The application displays "Too low. Try again." (Fig. 1.30), meaning that the value you entered is less than the number the application chose as the correct guess.

Fig. 1.30 Entering an initial guess and receiving feedback.

Step 8: Entering Another Guess

At the next prompt, enter 750 (Fig. 1.31). The application displays "Too low. Try again.", because the value you entered once again is less than the correct guess.

Fig. 1.31 Entering a second guess and receiving feedback.

Step 9: Entering Additional Guesses

Continue to play the game (Fig. 1.32) by entering values until you guess the correct number. When you guess correctly, the application displays "Excellent! You guessed the number."

Fig. 1.32 Entering additional guesses and guessing the correct number.

Playing the Game Again or Exiting the Application

After you guess the correct number, the application asks if you’d like to play another game. At the "Would you like to play again (y or n)?" prompt, entering the character y causes the application to choose a new number and displays the message "Please type your first guess." followed by a question-mark prompt so you can make your first guess in the new game. Entering the character n terminates the application. Each time you execute this application from the beginning (Step 6), it will choose the same numbers for you to guess.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset