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
Figure 1.2. Opening a Command Prompt window and changing the directory.
Figure 1.3. Running the GuessNumber application.
Figure 1.4. Entering your first guess.
(This item is displayed on page 18 in the print version)
Figure 1.5. Entering a second guess and receiving feedback.
Figure 1.6. Entering additional guesses and guessing the correct number.
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)
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.
then pressing Enter. The command cd is used to change directories.
~$ cd examples/ch01/GuessNumber/GNU_Linux ~/examples/ch01/GuessNumber/GNU_Linux$
g++ GuessNumber.cpp -o GuessNumber
as in Fig. 1.10. The preceding command compiles the application and produces an executable file called GuessNumber.
~/examples/ch01/GuessNumber/GNU_Linux$ g++ GuessNumber.cpp -o GuessNumber ~/examples/ch01/GuessNumber/GNU_Linux$
~/examples/ch01/GuessNumber/GNU_Linux$ ./GuessNumber I have a number between 1 and 1000. Can you guess my number? Please type your first 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. ?
~/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. ?
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)?
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. ?
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
Class string and String Stream Processing
Searching and Sorting
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
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