Mark the following statements as true or false.
An output stream is a sequence of characters from a computer to an output device. (1)
To use cin and cout in a
program , the program must include the header file iostream. (1, 2)Suppose pay is a variable of type double. The statement cin >> pay, requires the input of a decimal number. (2)
The statement cin >> length; and length >> cin; are equivalent. (2)
When the statement cin >> numl >> num2; executes, then after inputting a number into the variable numl the program skips all trailing whitespace characters. (2)
To use the predefined function sqrt in a program, the program must include the header file cmath. (3)
The statement cin.get (ch); inputs the next nonwhitespace character into the variable ch. (4)
When the input stream enters the fail state, the program terminates with an error message. (5)
To use the manipulators fixed and showpoint, the program does not require the inclusion of the header file iomanip. (6)
The statement cin >> right; sets the input of only the next variable right-justified. (7)
To input data from a file, the program must include the header file fstream. (10)
a.
An output stream is a stream from a computer to a destination.
An output stream is a sequence of characters from a computer to an output device. Hence, the given statement is “True”.
Explanation of Solution
An output stream is a stream from a computer to a destination, where the destination is an output device such as the computer screen (monitor), file, etc.
b.
To use cin and cout in a program, the program must include the header file iostream. This header file contains the definition of the input and output stream objects, cin and cout.
To use cin and cout in a program, the program must include the header file iostream. Hence, the given statement is “True”.
Explanation of Solution
To use cin and cout in a program, the program must include the header file iostream. This header file contains the definition of the input and output stream objects, cin and cout. So we can use these inbuilt objects provided by the standard header files of C++. To use cin and cout, every C++ program must use the preprocessor directive:
#include <iostream>
c.
The extraction operator >> is binary and thus takes two operands. The left-side operand must be an input stream variable, such as cin and the right-hand operand, a variable.
Suppose pay is a variable of type double, the statement cin >> pay; does not require the input to be only of a decimal number type. Hence, the given statement is “False”.
Explanation of Solution
The statement cin >> pay; can receive input of both integer and decimal types. If the input is of the integer type, it is converted into a decimal type with zero decimal part and then assigned to the pay variable of double type. If the input is decimal type, then the input is received and assigned to the variable without any conversion.
d.
The extraction operator >> is binary and thus takes two operands. The left-side operand must be an input stream variable, such as cin and the right-hand operand, a variable.
The statement cin >> length; and length >> cin; are not equivalent. Hence, the given statement is “False”.
Explanation of Solution
The extraction operator >> is binary and thus takes two operands. The left-side operand must be an input stream variable, such as cin and the right-hand operand, a variable. Since the order of the operands is fixed, the statement length >> cin; is not a valid C++ statement and the order of the operands is incorrect. cin >> length; is a syntactically correct statement.
e.
The extraction operator >> is binary and thus takes two operands. The left-side operand must be an input stream variable, such as cin and the right-hand operand, a variable. A single input statement can read more than one data item by using the operator >> several times. When scanning for the next input, >> skips all whitespace characters. Whitespace characters consist of blanks and non-printable characters, such as tabs and the newline character.
When the statement cin >> num1 >> num2; executes, then after inputting a number into the variable num1 the program skips all trailing whitespace characters. Hence, the given statement is “True”.
Explanation of Solution
A single input statement can read more than one data item by using the operator >> several times. When scanning for the next input, >> skips all whitespace characters. Whitespace characters consist of blanks and non-printable characters, such as tabs and the newline character. Hence, after inputting a number in the variable num1, the program skips all trailing whitespace characters and inputs the next number in the variable num2.
f.
C++ comes with a wealth of functions called predefined functions. These predefined functions are organized as a collection of libraries called header files. A particular header file may contain several functions. To use a particular function, one needs to know the name of the function, the return type and parameters to the function. cmath is one such header file which is part of the C++ system and contains functions such as sqrt, pow, etc.
To use the predefined function sqrt in a program, the program must include the header file cmath. Hence, the given statement is “True”.
Explanation of Solution
Since sqrt function is included as part of the header file cmath, it should be included using the preprocessor directive as below:
#include <cmath>
g.
The variable cin can access the stream function get, which is used to read character data. The get function inputs the very next character, including whitespace characters, from the input stream and stores it in the memory location passed to it as an argument. The syntax for the command is:
cin.get(varChar);
where carChar is a char type variable.
The statement cin.get(ch); can input either the next non-whitespace character or the next whitespace character in the variable ch. Hence, the given statement is “False”.
Explanation of Solution
The statement cin.get(ch); can input either the next non-whitespace character or the next whitespace character in the variable ch. So it is incorrect to say that only the next non-whitespace character is input.
h.
Attempting to read invalid data in a variable causes the input stream to enter the fail state. Once an input failure has occurred, the function clear can be used to restore the input stream to a working state. Once an input stream enters the fail state, all further I/O statements using that stream are ignored and the program continues to execute with whatever values are stored in variables and produces incorrect results.
When the input stream enters the fail state, the program does not terminate with an error message. Hence, the given statement is “False”.
Explanation of Solution
When the input stream enters the fail state, the program does not terminate with an error message, instead all further I/O statements using that stream are ignored and the program continues to execute with whatever values are stored in variables and produces incorrect results.
i.
To output floating-point numbers in a fixed decimal format, the manipulator fixed is used. When the computer is instructed to output the decimal number in a fixed decimal format, the output may not show the decimal point and the decimal part. To force the output to show the decimal point and trailing zeros, the manipulator showpoint is used.
To use the manipulators fixed and showpoint, the program does not require the inclusion of the header file iomanip. Hence, the given statement is “True”.
Explanation of Solution
To use the manipulators fixed and showpoint, the program does not require the inclusion of the header file iomanip. The iomanip header file is however required to use the manipulators set precision, setfill and setw.
j.
To right-justify the output and the manipulator right, the syntax to set the manipulator right is:
ostreamVar << right;
where ostreamVar is an output variable such as cout.
The statement cin >> right; sets the input of all the variables henceforth right-justified. Hence, the given statement is “False”.
Explanation of Solution
The statement cin >> right; sets the input of all the variables henceforth right-justified and not just the next variable. In order to change the setting, it has to be done explicitly using the syntax as follows:
cout.unsetf(ios::right);
k.
For file I/O, the statement #include <fstream> is to be used to include the header file fstream in the program. Also, variables of type ifstream for file input and of type ofstream for file output should be declared. open statements are then used to open input and output files. The header file fstream contains the definitions of ifstream and ofstream.
To input data from a file, the program must include the header file fstream. Hence, the given statement is “True”.
Explanation of Solution
To input data from a file, the program must include the header file fstream which contains the definition of ifstream and ofstream types. Variables of these types are then declared and used to open files for input and output.
Want to see more full solutions like this?
Chapter 3 Solutions
C++ Programming: From Problem Analysis to Program Design
- In Python plEASE The Payroll Department keeps a list of employee information for each pay period in a text file. The format of each line of the file is the following: <last name> <hours worked> <hourly wage> Write a program that inputs a filename from the user and prints to the terminal a report of the wages paid to the employees for the given period. The report should be in tabular format with the appropriate header. Each line should contain: An employee's name The hours worked The wages paid for that period. An example of the program input and output is shown below: Enter the file name: data.txt Name Hours Total Pay Lambert 34 357.00 Osborne 22 137.50 Giacometti 5 503.50arrow_forwardBy use python languagearrow_forwardThe Payroll Department keeps a list of employee information for each pay period in a text file. The format of each line of the file is the following: <last name> <hours worked> <hourly wage> Write a program that inputs a filename from the user and prints to the terminal a report of the wages paid to the employees for the given period in Python. The report should be in tabular format with the appropriate header. Each line should contain: An employee’s name The hours worked The wages paid for that period.arrow_forward
- In C language, 3. A text file name.txt - contains this data, Sam Davis Kobe S Bryant Michael K jordan Li Peng Raj Mohan Sharma Can you write a program to read this file and print each name separately in each line ?arrow_forwardUse the Python languagearrow_forwarduse c++ programming language Read two integer number from an input file called input.txt, then add these two numbers , store it in a variable called TOTAL, find the difference of the two numbers , store it in Difference, find product to the two numbers , store it in a variable called Multiply, fine quotient of the two number call it Division, find modular division of them and store it in a variable called MUD.arrow_forward
- Computer Science C++ Create a text file "input.txt" with a certain amount of integers (you decide how many). Write a program that reads these numbers from the file, adds them, and when you have reached the end of the file, calculates the average of these numbers. Print a message and the average to the console. Code this program twice, demonstrating the two methods to detect the end of the file, part A: reading a value from Instream and storing it (boolean expression) in the while loop part B: using the eof() member functionarrow_forward(Data processing) Write a C++ program that allows the user to enter the following information from the keyboard for each student in a class (up to 20 students): Name Exam 1 Grade Exam 2 Grade Homework Grade Final Exam Grade For each student, your program should first calculate a final grade, using this formula: FinalGrade=0.20Exam1+0.20Exam2+0.35Homework+0.25FinalExam Then assign a letter grade on the basis of 90100=A,8089=B,7079=C,6069=D, and less than 60=F . All the information, including the final grade and the letter grade, should then be displayed and written to a file.arrow_forward(Data processing) Write a C++ program that reads the file created in Exercise 4, permits the user to change the hourly wage or years for any employee, and creates a new updated file.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr