T U T O R I A L 2 Welcome Application Introduction to C++ Programming (Solutions) 1
2 Introduction to C++ Programming (Solutions) Tutorial 2 These solutions will not be available to students. Instructor s Manual Exercise Solutions Tutorial 2 MULTIPLE-CHOICE QUESTIONS 2.1 Use the object to display text on the screen. a) screen b) cout c) output d) iostream 2.2 To compile an application in Visual Studio.NET, select. a) File > Compile Solution b) Build > Compile Solution c) Build > Build Solution d) Compile > Build Solution 2.3 must begin the body of every function. a) A newline character b) The int keyword c) A return statement d) A left brace ({ ) 2. Syntax errors in a C++ application. a) can cause subtle errors when the application runs 2.5 Files with a.obj extension contain. 2. Operator << is the operator. 2.7 The escape sequence sounds the system bell. 2.8 To run an application using Visual Studio.NET, select. 2.9 A is an example of whitespace character. b) prevent the application from compiling correctly c) are ignored by the compiler d) are detected after the application has been compiled a) source code b) an executable image c) machine code d) preprocessor information a) stream insertion b) stream extraction c) display d) cout a) \b b) \a c) \sb d) None of the above. a) Run > Start Without Debugging b) Build > Start c) Debug > Start Without Debugging d) Run > Start a) tab b) space c) newline d) All of the above. 2.10 The escape sequence places the cursor at the beginning of the current line. a) \n b) \r c) \t d) \b Answers: 2.1) b. 2.2) c. 2.3) d. 2.) b. 2.5) c. 2.) a. 2.7) b. 2.8) c. 2.9) d. 2.10) b. EXERCISES 2. ( Alarm Application) Many universities provide computer networks that enable multiple users to access information on the local area network (LAN) and on the Internet. To increase security, the system will log off a user if that user has been inactive for a significant period of time. Suppose the system administrator wants the system to warn the user before logging off. In this exercise, you will create an application that displays a message to the user
Tutorial 2 Welcome Application 3 and sounds the system bell three times to indicate that the user will be logged off in one minute (Fig.2.35). Figure 2.35 Alarm application output for completed application. Answer: a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\Alarm to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project Alarm. Save this project in your SimplyCpp directory.then add the existing source code file, Alarm.cpp, which is located in the C:\SimplyCpp\Alarm directory. c) Opening the template file. Open the Alarm.cpp file in your text editor or IDE. d) Displaying a message to the user. Before sounding the system bell, you should display a warning message to the user.after line 9 in the template code, insert a statement to display a newline and the message, "Warning: You have been inactive for over 30 minutes.". Do not terminate the statement yet. Insert a second stream insertion operator on the next line of your application s source code to display the text, "You will be logged out in one minute." This text should be displayed on a new line, using the newline escape sequence. Two newlines should be inserted after this message is displayed. Remember to terminate the second line of your statement with a semicolon. e) Sounding the system bell. To sound the system bell, display a string that consists of three alert escape sequences. f) Save your modified source code file. g) Compile the completed application. h) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the program output to the one shown in Fig. 2.35 to ensure that you displayed the message to the user correctly. [Note: Depending on the system you are using, you may not hear the computer s system bell or the three bells may sound so quickly that you may hear the system bell sound only once when the application runs.] i) Close the Command Prompt window. j) Close your text editor or IDE. 1 // Exercise 2. 2 // Application that prints a message and sounds an alarm. 10 cout << "\nwarning: You have been inactive for over 30 minutes." << "\nyou will be logged out in one minute.\n\n";
Introduction to C++ Programming (Solutions) Tutorial 2 cout << "\a\a\a"; 1 15 return 0 ; // indicate that program ended successfully 1 17 } // end function main 2. ( Table of Powers Application) Throughout this book, you will be asked to create C++ applications that display tables of values. In this exercise, you will use the tab and newline escape sequences to display a table of the squares and cubes of the numbers from zero to 5 (Fig. 2.3). In a later tutorial, you will modify the application to calculate the values displayed in Fig.2.3. Figure 2.3 Table of Powers application output. a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\TableOfPowers to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project TableOfPowers. Save this project in your SimplyCpp directory. Then add the existing source code file, TableOfPowers.cpp, which is located in the C:\SimplyCpp\TableOfPowers directory. c) Opening the template file. Open the TableOfPowers.cpp file in your text editor. d) Displaying the table header. When displaying a table of values, each column of values typically is separated by at least one tab character.you should always begin the table with a header, which is a line of text that labels each column, so that the user can identify the contents of each column. In this application, the first column will display a number, the second column will display its square and the third column will display its cube. Display a line of text containing the words number, square and cube, separated by tab escape sequences.terminate the output string with a newline escape sequence. e) Displaying the values. For the first row of values, display three zeros, each separated by tab escape sequences.terminate the output string with a newline escape sequence. For the next five rows, use the values in Fig.2.3, separating each value by a tab escape sequence and ending each output string with a newline escape sequence.add a second newline escape sequence to the last output string. f) Save your modified source code file. g) Compile the completed application. h) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the output of your completed Table of Powers application with the output shown in Fig. 2.3 to ensure that you wrote your application correctly. i) Close the Command Prompt window. j) Close your text editor or IDE.
Tutorial 2 Welcome Application 5 Answer: 1 // Exercise 2. 2 // Create an application that displays a table of powers. 10 1 15 1 17 cout << "Number\tSquare\tCube\n"; cout << "0\t0\t0\n"; cout << "1\t1\t1\n"; cout << "2\t\t8\n"; cout << "3\t9\t27\n"; cout << "\t1\t\n"; cout << "5\t25\t5\n\n"; 18 return 0 ; // indicate that program ended successfully 19 20 } // end function main 2. (Restaurant Survey Application) In the next tutorial, you will learn how to create applications that retrieve input that the user types at the keyboard. To improve ease of use, many C++ applications include a numbered list of options, called a menu, from which users choose. Instead of typing the name of an option, the user simply enters the option s number. For example, many restaurants allow you to place an order using a number corresponding to a particular menu item. In this exercise, you display a menu of options for a restaurant (Fig.2.37). In later tutorials, you will create applications that respond to the user s selection. Figure 2.37 Restaurant Survey application output. a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\RestaurantSurvey to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project RestaurantSurvey. Save this project in your SimplyCpp directory. Then add the existing source code file, RestaurantSurvey.cpp, which is located in the C:\SimplyCpp\RestaurantSurvey directory. c) Opening the template file. Open the RestaurantSurvey.cpp file in your text editor or IDE. d) Displaying the menu header. Before displaying the menu, you should display a message instructing the user to select one of the options that will follow. Use a cout statement to display a blank line followed by the message, "Select a menu option", followed by a newline escape sequence.
Introduction to C++ Programming (Solutions) Tutorial 2 Answer: e) Displaying the first menu option. Each menu item should consist of the option s number and a description of the option. The options should be presented in increasing numerical order. In this case, the first option is a hamburger. Therefore, display the message "1 - Hamburger" and insert a newline escape sequence to terminate the line. f) Displaying the remaining menu options. The second menu option is Hot dog. Display the option number followed by its description, as you did in Step e. Similarly, display the third option, Salad. Finally, display the fourth option, which is Exit. Each option should appear on its own line. Finally, on a new line, display a question mark (? ), followed by a space.this is where the user will type input to make a selection. In the next tutorial, you will learn how to read the user s response from the keyboard. g) Save your modified source code file. h) Compile the completed application. i) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the output of your completed Restaurant Survey application with the output shown in Fig. 2.37 to ensure that you wrote your application correctly. j) Close the Command Prompt window. 1 // Exercise 2. 2 // Create an application that displays a menu of options. 10 1 15 1 cout << "\nselect a menu option\n"; cout << "1 - Hamburger\n"; cout << "2 - Hot dog\n"; cout << "3 - Salad\n"; cout << " - Exit\n"; cout << "? \n\n"; 17 return 0 ; // indicate that program ended successfully 18 19 } // end function main What does this code do? 2.1 What does the following code do? In particular, what does it display? 1 // Exercise 2.1: WDTCD.cpp 2 // What does this code do? 10 cout << "\n *\n ***\n*****\n ***\n *\n\n"; return 0 ; // indicate that program ended successfully 1 } // end function main
Tutorial 2 Welcome Application 7 Answer: The application displays a diamond. What s wrong with this code? 2.15 The following code segment should display a table listing an item for sale in the first column and the item s price in the second column (Fig.2.38). Figure 2.38 Correct application output. Find the error(s) in the following code: 1 // Exercise 2.15: WWWTC.cpp 2 // This application displays prices for certain items 10 cout << "Item\tPrice"; cout << "Hammer\t$10"; cout << "Nails\t$2"; cout << "Saw\t$15"; 1 15 return 0 ; // indicate that program ended successfully 1 17 } // end function main Answer: In the original code, the output statement is missing a newline escape sequence to allow the next item to be displayed on the next line.also, the first line of Fig. 2.38 indicates that there should be a blank line before the table is displayed. So, there is also a newline missing at the beginning of the corrected code is displayed below. Modified lines are highlighted. 1 // Exercise 2.15: WWWTC.cpp 2 // This application displays prices for certain items 3 // in a store #include <iostream> // required to perform C++ stream I/O 5 using namespace std; // for accessing C++ Standard Library members 7 8 // function main begins program execution 9 int main() 10 { cout << "\nitem\tprice\n";
8 Introduction to C++ Programming (Solutions) Tutorial 2 1 15 cout << "Hammer\t$10\n"; cout << "Nails\t$2\n"; cout << "Saw\t$15\n\n"; 1 return 0 ; // indicate that program ended successfully 17 18 } // end function main Programming Challenge 2.1 (Enhanced Welcome Application) The Welcome application you created in this tutorial displayed plain text. A series of symbols (such as asterisks) can be used to display simple graphics. In this application, you will modify the Welcome application to display a message containing a border, centered text and the word C++ displayed using asterisks (Fig.2.39). Figure 2.39 Enhanced Welcome application output. a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\EnhancedWelcome to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project EnhancedWelcome. Save this project in your SimplyCpp directory. Then add the existing source code file, Welcome.cpp, which is located in the C:\SimplyCpp\EnhancedWelcome directory. c) Opening the template file. Open the Welcome.cpp file in your text editor or IDE. d) Creating the top border. The first line of output should be the top border for your text. In the template code, insert a cout statement that displays a newline, then 3 asterisks followed by a newline. e) Creating the second line of output. To create the left and right borders, each output line should begin and end with an asterisk. Also, each output line must be exactly 3 characters wide. The second line of output does not contain any text, so the line should output an asterisk, followed by 32 spaces, an asterisk and a newline escape sequence (in that order). f) Displaying Welcome to. To center the text Welcome to, display spaces after the first asterisk before displaying Welcome to. Remember to insert a newline at the end of every line you display. g) Displaying C++ Display a line containing only the left and right borders.then use the next seven lines of output to create the C++ out of asterisks, as shown in Fig. 2.39. Then, display a line containing only the left and right borders. Display the bottom border, which should appear exactly as the top border from Step d. Finally, display two newlines. h) Save your modified source code file.
Tutorial 2 Welcome Application 9 Answer: i) Compile the completed application. j) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the output of your completed Welcome application with the output shown in Fig. 2.39 to ensure that you wrote your application correctly. k) Close the Command Prompt window. l) Close your text editor or IDE. m)(optional) Using a single statement to display the welcome message.try displaying the entire welcome message using a single statement. Note that your statement may continue on multiple lines of source code when you use multiple stream insertion operators. 1 // Exercise 2.1 2 // Create an application that displays a welcome message 10 1 15 1 17 18 19 20 21 22 23 cout << "\n**********************************\n"; cout << "* *\n"; cout << "* Welcome to *\n"; cout << "* *\n"; cout << "* *** * *\n"; cout << "* * * * *\n"; cout << "* * * * * *\n"; cout << "* * *** *** * *\n"; cout << "* * * * * *\n"; cout << "* * * *\n"; cout << "* *** * *\n"; cout << "* *\n"; cout << "**********************************\n\n"; 2 return 0 ; // indicate that program ended successfully 25 2 } // end function main