Thursday, October 15, 2009

1.7. Test-Driving a C++ Application



1.7. Test-Driving a C++ Application


In this section, you'll run and interact
with your first C++ application. You'll begin by running 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: This 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 in two ways—using 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 Eclipse, GNU C++, Microsoft Visual C++,
etc.


In the following steps, you'll run the
application and enter various numbers to guess the correct number. 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 filenames, 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 key term is set in bold italic. 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 Command Prompt colors 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
    book to ensure that you've copied the book's examples to your hard
    drive.




  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:\examples\ch01\GuessNumber\Windows
    , then press Enter (Fig. 1.2). The command cd is
    used to change directories.




    Fig. 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.]




    Fig. 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)




    Fig. 1.4. Entering your first guess.







  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 application chose as the correct guess.




    Fig. 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. The application will display
    "Excellent! You guessed the number!" (Fig. 1.6).




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







  7. Playing the game again or exiting
    the application.
    After you guess correctly, 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 type
    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.





    Fig. 1.7. Playing the game
    again.







    Fig. 1.8. Exiting the game.







  8. Close the Command Prompt window.


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


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 Examples/ch01/GuessNumber/GNU_Linux

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











    Fig. 1.9. Changing to the GuessNumber application's directory after logging
    in to your 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, you must first compile it by typing


    g++ GuessNumber.cpp -o GuessNumber

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











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

    ~/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).











    Fig. 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.]











    Fig. 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.











    Fig. 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 correctly, the application displays "Excellent! You
    guessed the number."
    (Fig. 1.14).












    Fig. 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 you guess 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 type 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.











    Fig. 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.
    ?












    Fig. 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$



 


No comments:

Post a Comment