Objectives Using Cout, the Endl Manipulator, and Common Escape
Total Page:16
File Type:pdf, Size:1020Kb
LAB 2.5 USING COUT, THE ENDL MANIPULATOR, AND COMMON ESCAPE SEQUENCES In C++, to output on the standard output device (usually the screen), you use cout and the stream insertion operator, <<. C++ evaluates the expression used with the stream insertion operator and prints its value at the current cursor position on the output device.You use a manipulator to format the output.The endl (the lowercase letter el, not the number 1) manipulator moves the cursor to the beginning of the next line and is called a newline character.You can accomplish the same action using the newline escape sequence \n as a character by itself or within a string.You can use multiple stream insertion operators in a single cout statement. Do not use a line wrap if an output string is too long. Close the string, go to the next line, use another insertion operator, and start a new string. If you break a string this way,remember to include blanks if appropriate. There are many escape sequences in C++ that allow you to control the output. The following are some of the commonly used escape sequences: \n Newline Cursor moves to the beginning of the next line \t Tab Cursor moves to the next tab stop \b Backspace Cursor moves one space to the left \r Return Cursor moves to the beginning of the current line (not the next line) \\ Backslash Backslash is printed \’ Single quotation Single quotation mark is printed \” Double quotation Double quotation mark is printed Comments appear in a program as explanations to the programmer and are ignored by the compiler. Objectives In this lab, you learn to use cout and the insertion stream operator to display expressions or manipu- lators on the screen. After completing this lab, you will be able to: I Display expressions on the screen. I Use the endl manipulator. I Use common escape sequences. Estimated completion time: 30–40 minutes Using cout, the endl Manipulator, and Common Escape Sequences Describe the following declaration, assignment, input, and the output statements or write C++ code to implement the comment statements. Describe what is contained in memory and show what is dis- played to the screen after each cout statement is executed. 1. const double PI = 3.1416; double radius = 2.5; cout << “The area of the circle is ” << PI * radius * radius << endl; 2. Write the appropriate C++ statement to match the description in each of the following comments: // declare an integer variable for the width of a rectangle // declare an integer variable for the length of a rectangle // prompt the user for the width and length // input the width and length values // output the area of the rectangle found by multiplying length times width 3. Indicate the purpose of the declaration statements and each pair of input and output statements. doubleƒbalance; doubleƒdeposit; doubleƒchecks; coutƒ<<ƒ“Enterƒyourƒbeginningƒcheckbookƒbalance:ƒ”; cinƒ>>ƒbalance;ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ//assumeƒ536.25ƒisƒentered coutƒ<<ƒ“Enterƒyourƒdepositsƒforƒtheƒmonth:ƒ”; cinƒ>>ƒdeposit;ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ//assumeƒ64.75ƒisƒentered coutƒ<<ƒ“Enterƒyourƒchecksƒforƒtheƒmonth:ƒ”; cinƒ>>ƒchecks;ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ//assumeƒ425.50ƒisƒentered coutƒ<<ƒ“Yourƒendingƒcheckƒbalanceƒis:ƒ”ƒ<<ƒbalanceƒ+ƒdepositƒ–ƒchecksƒ <<ƒendl; 4. Write the appropriate C++ statement to match the description in each of the following comments: // declare three double variables called num1, num2, num3 // declare a variable called average // ask the user to input values separated by a space for 3 values // input the values into num1, num2, num3 // assign average the value of num1, num2, and num3 divided by 3 // output a statement with a message describing the values input and the average 5. When a value is assigned in memory,whatever value was previously in memory is replaced by the new assigned value.This is called destructive replacement. If you want two memory locations to exchange values, you need to save a value in a third location to prevent losing the original value. The following code shows how to swap two values in memory.Explain the purpose of each of the following lines: intƒnum1ƒ=ƒ10,ƒnum2ƒ=ƒ5,ƒtemp; coutƒ<<ƒ“num1ƒisƒ”ƒ<<ƒnum1ƒ<<“,ƒnum2ƒisƒ”<<ƒnum2ƒ<<ƒendl; tempƒ=ƒnum1; num1ƒ=ƒnum2; num2ƒ=ƒtemp; coutƒ<<ƒ“Nowƒtheƒnumbersƒswapƒandƒnum1ƒisƒ”ƒ<<ƒnum1ƒ<<“,ƒnum2ƒisƒ”ƒ ƒƒƒƒƒ<<ƒnum2ƒ<<ƒendl; 6. Write the appropriate C++ statements to match the descriptions in the following comments: // declare two integer variables feet and inches // ask the user how many feet and inches tall they are (no fraction) // input the feet and inches // assign inches the value of inches plus feet times 12 // output with an appropriate message the number of inches the person is tall 7. Write the output of the following code: intƒxƒ=ƒ2,ƒyƒ=ƒ3,ƒzƒ=ƒ5; coutƒ<<ƒ“Thisƒisƒmyƒreportƒinƒcolumns\n” ƒƒƒƒƒ<<ƒ'\t'ƒ<<ƒxƒ<<ƒ'\t'ƒ<<ƒxƒ*ƒxƒ<<ƒendl ƒƒƒƒƒ<<ƒ'\t'ƒ<<ƒyƒ<<ƒ'\t'ƒ<<ƒyƒ*ƒyƒ<<ƒendl ƒƒƒƒƒ<<ƒ'\t'ƒ<<ƒzƒ<<ƒ'\t'ƒ<<ƒzƒ*ƒzƒ<<ƒendl; 8. Write a C++ statement to match the descriptions in the following comments: // output a heading that has your name used as a possessive noun (use a single quote) // followed by the words favorite movie // output a newline that shows your favorite movie title in double quotes.