Ch. 6 Self-Test Exercises

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What include directive do you need to use to place in your program file if your program uses the function exit?

#include <cstdlibrary>

What output will be produced when the following lines are executed? cout << "*" << setw(5) << 123; cout.setf(ios::left); cout << "*" << setw(5) << 123; cout.setf(ios::right); cout << "*" << setw(5) << 123 << "*" << endl;

* 123*123 * 123*

What output will be produced when the following lines are executed? ofstream fout; fout.open("stuff.dat"); fout << "*" << setw(5) << 123 << "*" << 123 << "*" << endl; fout.setf(ios::showpos); fout << "*" << setw(5) << 123 << "*" << 123 << "*" << endl;

* 123*123* * +123*+123*

What output will be produced when the following lines are executed? cout << "*" << setw(5) << 123 << "*" << 123 << "*" << endl; cout.setf(ios::showpos); cout << "*" << setw(5) << 123 << "*" << 123 << "*" << endl; cout.unsetf(ios::showpos); cout.setf(ios::left); cout << "*" << setw(5) << 123 << "*" << setw(5) << 123 << "*" << endl;

* 123*123* * +123*+123* *123 *123 *

What output will be produced when the following lines are executed? cout << "*"; cout.width(5); cout << 123 << "*" << endl; cout << "*" << setw(5) << 123 << "*" << endl;

* 123*123* * 123*123*

What output will be produced when the following line is executed? cout << "*" << setw(3) << 12345 << "*" endl;

*12345*

What output will be produced when the following lines are executed? ifstream ins; ins.open("list.dat"); int count = 0, next; while (ins >> next) { count++; cout << next << endl; } ins.close( ); cout << count; The file list.dat contains the following three numbers (and nothing more): 1 2 3

1 2 3 3

What characteristics of files do ordinary program variables share? What characteristics of files are different from ordinary variables in a program?

Both files and program variables stores values and can have values retrieved from them. Program variables exist only while the program runs, whereas files may exist before a program is run and may continue to exist after a program stops. In short, files may be permanent; variables are not. Files provide the ability to store large quantities of data, whereas program variables do not provide quite so large a store.

Suppose ins is a file input stream that has been connected to a file with the member function open. Suppose your program has just read the last character in the file. At this point, would ins.eof( ) evaluate to true or false?

It would evaluate to false. Your program must attempt to read one more character (beyond the last character) before it changes to true.

In formatting output, the following flag constant is used with the stream member function setf. What effect does it have? ios::showpos

Setting this flag causes a plus sign to be output before positive integer values

In formatting output, the following flag constant is used with the stream member function setf. What effect does it have? ios::scientific

Setting this flag causes floating point numbers to be displayed in e-notation, that is, in scientific notation. Setting this flag unsets ios::fixed.

In formatting output, the following flag constant is used with the stream member function setf. What effect does it have? ios::left

Setting this flag causes subsequent output to be placed at the left end of any field that is set with the width member function. That is, any extra blanks are put after the output. Setting this flag unsets ios::right

In formatting output, the following flag constant is used with the stream member function setf. What effect does it have? ios:: right

Setting this flag causes subsequent output to be placed at the right end of any field that is set with the width member function. That is, any extra blanks are put before the output. Setting this flag unsets ios::left

In formatting output, the following flag constant is used with the stream member function setf. What effect does it have? ios::showpoint

Setting this flag causes the decimal point and trailing zeros to be always displayed

The putback member function "puts back" a symbol into an input stream. Does the symbol that is put back have to be the last symbol input from the stream? For example, if your program reads an 'a' from the input stream, can it use the putback function to put back a 'b', or can it only put back an 'a'?

The character that "put back" into the input stream with the member function putback need not be the last character read. If your program reads an 'a' from the input stream, it can use the putback function to put back a 'b.' The text in the input file will not be changed by putback, although your program will behave as if the text in the input file has been changed.

Consider the following code: char c1, c2, c3, c4; cout << "Enter a line of input: \n"; cin.get(c1); cin.get(c2); cin.get(c3); cin.get(c4); cout << c1 << c2 << c3 << c4 << "END OF OUTPUT"; If the dialogue begins as follows, what will be the next line of output? **************************** Enter a line of input: a b c d e f g

The complete dialogue is Enter a line of input: a b c d e f g a b END OF OUTPUT

Consider the following code: char next; int count = 0; cout << "Enter a line of input: \n"; cin.get(next); while (next != '\n') { if ((count % 2) == 0) cout << next; count++; cin.get(next); } If the dialogue begins as follows, what will be the next line of output? **************************** Enter a line of input: abcdef gh

The complete dialogue is: Enter a line of input: abcdef gh ace h

What does exit(1) do with its argument?

The exit(1) function returns the argument to the operating system.

What is the difference between: cin >> c; and cin.get(c);

The statement cin >> c; reads the nonwhite characters and skips blank spaces, tabs, and newline characters. The statement cin.get(c) reads the next character whether the character is nonwhite or not.

In the text it says "a file has two names." What are the two names? When is each name used?

The two names are the external file name and the stream name. The external file name is used only in the call to the function open, which connects the file to a stream. After the call to open, your program always uses the stream name as the name of the file.

Suppose c is a variable of type char. What is the difference between: cout << c; and cout.put(c)

The two statements are equivalent. Both of the statements output the value of the variable c.

A program has read half of the lines in a file. What must the program do to the file to enable reading the first line a second time?

This is the "starting over" the text describes at the beginning of this chapter. The file must be closed and opened again. This action puts the read position at the start of the file ready to be read again.

What output does the following function provide in response to the following calls? void func(double x, double y = 1.1, double z = 2.3) { cout << x << " " << y << " " << z << endl; } Calls: a. func(2.0); b. func(2.0, 3.0) c. func(2.0, 3.0, 4.0);

a. 2.0 1.1 2.3 b. 2.0 3.0 2.3 c. 2.0 3.0 4.0

Suppose bla is an object, dobedo is a member function of the object bla, and dobedo takes one argument of type int. How do you write a call to the member function dobedo of the object bla using the argument 7?

bla.dobedo(7);

Given: ifstream fin; ofstream fout; fin.open("stuff1.dat"); if (fail( )) { cout << "File failed to open. \n"; exit(1); } fout.open("stuff2.dat"); if (fail( )) { cout << "File failed to open. \n"; exit(1); } Suppose you reach the point at which you no longer need to get input from the file stuff1.dat and no longer need to send output to the file stuff2.dat. How do you close these files?

fin.close( ); fout.close( );

Given: ifstream fin; ofstream fout; Suppose you want to take a program's input from the file stuff1.dat and send its input to the file stuff2.dat. What statements do you need to place in your program in order to connect the stream fout to the file stuff2.dat? Be sure to include checks to make sure that the openings were successful.

fin.open("stuff1.dat"); if (fail( )) { cout << "File failed to open. \n"; exit(1); } fout.open("stuff2.dat"); if (fail( )) { cout << "File failed to open. \n"; exit(1); }

Suppose you are writing a program that uses a stream called fin that will be connected to an input file, and a stream called fout that will be connected to an output file. How do you declare fin and fout?

ifstream fin; ofstream fout;

In formatting output, the following flag constant is used with the stream member function setf. What effect does it have? ios::fixed

setting this flag causes floating point numbers not to be displayed in e-notation, that is, not in scientific notation. Setting this flag unsets ios::scientific.

Define a function called copyChar that takes one argument that is an input stream. When called, copyChar will read one character of input from the input stream given as its argument and will write that character to the screen. You should be able to call your function using either cin or an input-file stream as the argument to your function copyChar. If the argument is an input-file stream, then the stream is connected to a file before the function is called, so copyChar will not open or close any files. For example, the first of the following two calls to copyChar will copy a character from the file stuff.dat to the screen, and the second will copy a character from the keyboard to the screen: ifstream.fin; fin.open("stuff.dat"); copyChar(fin); copyChar(cin);

void copyChar(istream& sourceFile) { char next; sourceFile.get(next); cout << next; }

Define a function called copyLine that takes one argument that is an input stream. When called, copyLine reads one line of input from the input stream given as its argument and writes that line to the screen. You should be able to call your function using either cin or an input-file stream as the argument to your function copyLine. If the argument is an input-file stream, then the stream is connected to a file before the function is called, so copyLine will not open or close any files. For example, the first of the following two calls to copyLIne will copy a line from the file stuff.dat to the screen, and the second will copy a line from the keyboard to the screen: ifstream.fin; fin.open("stuff.dat"); copyLine (fin); copyLine(cin);

void copyLine(istream& sourceFile) { char next; do { sourceFile.get(next); cout << next; } while (next != '\n'); }

Define a function called sendLine that takes one argument that is an output stream. When called, sendLine reads one line of input from the keyboard and outputs the line to the output stream given as its argument. You should be able to call your function using either cout or an output-file stream as the argument to your function sendLine. If the argument is an output-file stream, then the stream is connected to a file before the function is called, so sendLine will not open or close any files. For example, the first of the following calls to sendLine copies a line from the keyboard to the file morestuf.dat and the second copies a line from the keyboard to the screen: ofstream fout; fout.open("morestuf.dat"); cout << "Enter 2 lines of input: \n"; sendLine(fout); sendLine(cout);

void sendLine(ostream& targetStream) { char next; do { cin.get(next); targetStream << next; } while (next != '\n'); }

Write the definition for a void function called textToScreen that has one formal parameter called fileStream that is of type ifstream. The precondition and postcondition for the function are as follows: Pre-condition: The stream fileStream has been connected to a file with a call to the member function open. Post-condition: The contents of the file connected to fileStream have been copied to the screen character by character, so that the screen output is the same as the contents of the text in the file. This function does not close the file

void textToScreen(ifstream& fileStream) { char next; fileStream.get(next); while ( ! fileStream.eof( )) { cout << next; fileStream.get(next); } }

Write the definition for a void function called toScreen. The function toScreen has one formal parameter called fileStream, which is of type ifstream. Given the following: Pre-condition: The stream fileStream has been connected to a file with a call to the member function open. The file contains a list of integers and nothing else. Post-condition: The numbers in the file connected to fileStream have been written to the screen one per line. (This function does not close the file.)

void toScreen(ifstream& fileStream) { int next; while (fileStream >> next) cout << next << endl; }


Ensembles d'études connexes

Psych 1101 - CITI - Human Subjects & Behavioral Research

View Set

Academic Decathlon 2019-2020: Art

View Set

Creencias e Ideologias Vocabulario

View Set

Estate Planning Chapter 11 - Buy/Sell Agreements

View Set

Exam 1 (excluding chapter 2 and 14)

View Set

BIOL 120 - The Lac Operon Lab Quiz Terms

View Set