Test-Driving a C++ Application

In this section, you will run and interact with your first C++ application. You will begin by running an entertaining guess-the-number game, which picks a number from 1 to 1000 and prompts you to guess the number. 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 have modified this application from the exercise you will be asked to create in Chapter 6, Functions and an Introduction to Recursion. Typically this application selects different numbers for you to guess each time you run it, because it chooses the numbers to guess at random. Our modified application chooses the same "correct" guesses every time you execute the program. This allows you to use the same guesses and see the same results that we show as we walk you through interacting with your first C++ application.]

We will demonstrate running a C++ application in two waysusing the Windows XP Command Prompt and using a shell on Linux (similar to a Windows Command Prompt). The application runs similarly on both platforms. Many development environments are available in which readers can compile, build and run C++ applications, such as Borland's C++Builder, Metrowerks, GNU C++, Microsoft Visual C++ .NET, etc. While we don't test-drive each of these environments, we do provide information in Section 1.19 regarding free C++ compilers available for download on the Internet. Please see your instructor for information on your specific development environment. Also, we provide several Dive-Into™ Series publications to help you get started with various C++ compliers. These are available free for download at www.deitel.com/books/cpphtp5/index.html.

In the following steps, you will 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 will learn to program in this book. Throughout the book, we use fonts to distinguish between features you see on the screen (e.g., the Command Prompt) and elements that are not directly related to the screen. Our convention is to emphasize screen features like titles and menus (e.g., the File menu) in a semibold sans-serif Helvetica font and to emphasize file names, text displayed by an application and values you should enter into an application (e.g., GuessNumber or 500), in a sans-serif Lucida font. As you have noticed, the defining occurrence of each term is set in blue, heavy bold. For the figures in this section, we highlight the user input required by each step and point out significant parts of the application. To make these features more visible, we have modified the background color of the Command Prompt window (for the Windows test-drive only). To modify the colors of the Command Prompt on your system, open a Command Prompt, then right click the title bar and select Properties. In the "Command Prompt" Properties dialog box that appears, click the Colors tab, and select your preferred text and background colors.]


Running a C++ application from the Windows XP Command Prompt

  1. Checking your setup. Read the Before You Begin section at the beginning of this textbook to make sure that you have copied the book's examples to your hard drive correctly.
  2. Locating the completed application. Open a Command Prompt window. For readers using Windows 95, 98 or 2000, select Start > Programs > Accessories > Command Prompt. For Windows XP users, select Start > All Programs > Accessories > Command Prompt. To change to your completed GuessNumber application directory, type cd C:examplesch01GuessNumberWindows, then press Enter (Fig. 1.2). The command cd is used to change directories.

    Figure 1.2. Opening a Command Prompt window and changing the directory.

  3. Running the GuessNumber application. Now that you are in the directory that contains the GuessNumber application, type the command GuessNumber (Fig. 1.3) and press Enter. [Note: GuessNumber.exe is the actual name of the application; however, Windows assumes the .exe extension by default.]

    Figure 1.3. Running the GuessNumber application.

  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.3). At the prompt, enter 500 (Fig. 1.4).

    Figure 1.4. Entering your first guess.

    (This item is displayed on page 18 in the print version)

  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. So, you should enter a lower number for your next guess. At the prompt, enter 250 (Fig. 1.5). The application again displays "Too high. Try again.", because the value you entered is still greater than the number that the correct guess.


    Figure 1.5. Entering a second guess and receiving feedback.

  6. Entering additional guesses. Continue to play the game by entering values until you guess the correct number. Once you guess the answer, the application will display "Excellent! You guessed the number!" (Fig. 1.6).

    Figure 1.6. Entering additional guesses and guessing the correct number.

  7. Playing the game again or exiting the application. After guessing the correct number, the application asks if you would like to play another game (Fig. 1.6). 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 enter your first guess." followed by a question mark prompt (Fig. 1.7) so you can make your first guess in the new game. Entering the character n ends the application and returns you to the application's directory at the Command Prompt (Fig. 1.8). Each time you execute this application from the beginning (i.e., Step 3), it will choose the same numbers for you to guess.

    Figure 1.7. Playing the game again.

    (This item is displayed on page 19 in the print version)

    Figure 1.8. Exiting the game.

    (This item is displayed on page 19 in the print version)

  8. Close the Command Prompt window.

Running a C++ Application Using GNU C++ with Linux

For this test drive, we assume that you know how to copy the examples into your home directory. Please see your instructor if you have any questions regarding copying the files to your Linux system. Also, for the figures in this section, we use a bold highlight to point out the user input required by each step. 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.

  1. Locating the completed application. From a Linux shell, change to the completed GuessNumber application directory (Fig. 1.9) by typing

    cd Examplesch01GuessNumberGNU_Linux
    

    then pressing Enter. The command cd is used to change directories.

    Figure 1.9. Changing to the GuessNumber application's directory after logging in to your Linux account.

     ~$ cd examples/ch01/GuessNumber/GNU_Linux
     ~/examples/ch01/GuessNumber/GNU_Linux$
    
     
  2. Compiling the GuessNumber application. To run an application on the GNU C++ compiler, it must first be compiled by typing

    g++ GuessNumber.cpp -o GuessNumber
    

    as in Fig. 1.10. The preceding command compiles the application and produces an executable file called GuessNumber.

    Figure 1.10. Compiling the GuessNumber application using the g++ command.

    (This item is displayed on page 20 in the print version)

     ~/examples/ch01/GuessNumber/GNU_Linux$ g++ GuessNumber.cpp -o GuessNumber
     ~/examples/ch01/GuessNumber/GNU_Linux$
    
     
  3. Running the GuessNumber application. To run the executable file GuessNumber, type ./GuessNumber at the next prompt, then press Enter (Fig. 1.11).


    Figure 1.11. Running the GuessNumber application.

     ~/examples/ch01/GuessNumber/GNU_Linux$ ./GuessNumber
     I have a number between 1 and 1000.
     Can you guess my number?
     Please type your first guess.
     ?
    
     
  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.11). At the prompt, enter 500 (Fig. 1.12). [Note: This is the same application that we modified and test-drove for Windows, but the outputs could vary, based on the compiler being used.]

    Figure 1.12. Entering an initial guess.

     ~/examples/ch01/GuessNumber/GNU_Linux$ ./GuessNumber
     I have a number between 1 and 1000.
     Can you guess my number?
     Please type your first guess.
     ? 500
     Too high. Try again.
     ?
    
     
  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.12). At the next prompt, enter 250 (Fig. 1.13). This time the application displays "Too low. Try again.", because the value you entered is less than the correct guess.

    Figure 1.13. Entering a second guess and receiving feedback.

     ~/examples/ch01/GuessNumber/GNU_Linux$ ./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.
     ?
    
     
  6. Entering additional guesses. Continue to play the game (Fig. 1.14) by entering values until you guess the correct number. When you guess the answer, the application displays "Excellent! You guessed the number!" (Fig. 1.14).


    Figure 1.14. Entering additional guesses and guessing the correct number.

     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)?
    
     
  7. Playing the game again or exiting the application. After guessing the correct number, the application asks if you would 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 enter your first guess." followed by a question mark prompt (Fig. 1.15) so you can make your first guess in the new game. Entering the character n ends the application and returns you to the application's directory in the shell (Fig. 1.16). Each time you execute this application from the beginning (i.e., Step 3), it will choose the same numbers for you to guess.

    Figure 1.15. Playing the game again.

     Excellent! You guessed the number.
     Would you like to play again (y or n)? y
    
     I have a number between 1 and 1000.
     Can you guess my number?
     Please type your first guess.
     ?
    
     

    Figure 1.16. Exiting the game.

     Excellent! You guessed the number.
     Would you like to play again (y or n)? n
    
     ~/examples/ch01/GuessNumber/GNU_Linux$
    
     

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net