Chapter 12 C++

Ace your homework & exams now with Quizwiz!

Write the necessary preprocessor directive to enable the use of file streams.

#include <fstream>

Given an ofstream object named output, associate it with a file named yearsummary.txt by opening the file for writing.

output.open("yearsummary.txt");

Given the availability of an ofstream object named output, write the other statements necessary to write the string "3.14159" into a file called pi. (Do not define a main function.)

output.open("pi"); output << "3.14159"; output.close();

Write a statement that reads 5 successive integers into these variables that have already been declared: x1 x2x3x4 x5. Then write a statement that prints each out on its own line so that they form a right-justified column with a 5-digit width. If any of the integers are 5-digits in size, then they will start at the very beginning of their lines. For example: |54213 | 8713 | 23 | 147 | 15 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cin >> x1 >> x2 >> x3 >> x4 >> x5; cout << setw(5) << right << x1 << "\n"; cout << setw(5) << right << x2 << "\n"; cout << setw(5) << right << x3 << "\n"; cout << setw(5) << right << x4 << "\n"; cout << setw(5) << right << x5 << "\n";

Given the availability of an ofstream object named output, and a string variable name tweet, write the other statements necessary to open a file named "mytweet", display the prompt tweet: and then read an entire line into tweet and then write it out to the file mytweet. (Do not define a main function.)

cout << "tweet:"; getline(cin,tweet); output.open("mytweet"); output << tweet; output.close();

Given three variables, k, m, n, of type int that have already been declared and initialized, write some code that prints each of them left-justified in a 9-position field on the same line. For example, if their values were 27, 987654321, -4321, the output would be: |27xxxxxxx987654321-4321xxxx NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout << left << setw(9) <<k; cout << left << setw(9) <<m; cout << left << setw(9) <<n;

Given three variables, a, b, c, of type double that have already been declared and initialized, write some code that prints each of them in a 15 position field on the same line, in such a way that scientific (or e-notation or exponential notation) is avoided. Each number should be printed with 5 digits to the right of the decimal point. For example, if their values were 24.014268319, 14309, 0.00937608, the output would be: |xxxxxxx24.01427xxxx14309.00000xxxxxxxx0.00938 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout << setw(15) << setprecision(5) << fixed <<a; cout << setw(15) << setprecision(5) << fixed <<b; cout << setw(15) << setprecision(5) << fixed <<c;

Given the availability of an ifstream object named indata and an ofstream object named outdata, write the other statements necessary to read one integer from a file called currentsales and write twice its value into a file called projectedsales. Assume that this is the extent of the input and output that this program will do.

double sales=0.0; indata.open("currentsales"); outdata.open("projectedsales"); indata >> sales; outdata << sales * 2.0; indata.close(); outdata.close();

Given an int variable x write some statements that attempt to open a file named "table20" and read a value into x; if that turns out not to be possible your code should then read avalue from standard input into x.

ifstream filename; filename.open("table20"); if (filename.fail()) { cin >> x; } else { filename >> x; }

Given a bool variable isReadable write some statements that assign true to isReadable if the file "topsecret" exists and can be read by the program and assigns false to isReadable otherwise.

ifstream filename; filename.open("topsecret"); if (filename.fail()) { isReadable=false; } else { isReadable=true; }

Define an object named infile that can be used to read data into program variables from a file.

ifstream infile;

Given the availability of an ifstream object named input, write the other statements necessary to read an integer into a variable datum that has already been declared from a file called rawdata. Assume that reading that one integer is the only operation you will carry out with this file. (Note: write just the statements, do not define a main function.)

input.open("rawdata"); input >> datum; input.close();

Given an ifstream object named input1, associate it with a file named winterdata.txt by opening the file for reading.

input1.open("winterdata.txt");

Given the availability of a file named numbers write the statements necessary to read an integer from standard input and then read in that many values from numbers and display their total.

int number, sum=0; #include <fstream> std::ifstream infile("numbers"); while (infile >> number) { sum += number; } cout << sum << endl;

Given three variables, k, m, n, of type int that have already been declared and initialized, write some code that prints each of them in a 9 position field on the same line. For example, if their values were 27, 987654321, -4321, the output would be: |xxxxxxx27987654321xxxx-4321 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout << setw(9) <<k; cout << setw(9) <<m; cout << setw(9) <<n;

Use an ifstream object named indata to read the first three integers from a file called lottowins and write each number to standard output, on a line by itself. Assume that this is the extent of the input that this program will do.

int x; indata.open("lottowins"); indata >> x; cout << x << endl; indata >> x; cout << x << endl; indata >> x; cout << x << endl; indata.close();

Define an object named outfile that can be used to write data from program variables to a file.

ofstream outfile;

Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do.

string name; int age; cin >> name >> age; outdata.open("outdata"); outdata << name << " " << age; outdata.close();


Related study sets

EAQ Study questions for MedSurg EXAM 3

View Set

Unit 27 - Communications with the Public

View Set

Retirement Planning: Plan Selection for Businesses (Module 8)

View Set