Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

9: Input and Output Streams

Output your birth year in decimal, hexadecimal, and octal form. Label each value. Line up your output in columns using the tab character. Now output your age.

See what that last statement prints, and explain it. Try some other formats.

Start a program called Test_output.cpp. Declare an integer birth_year and assign it the year you were born.

Output your birth_year in decimal, hexadecimal, and octal form.

Label each value with the name of the base used.

Did you line up your output in columns using the tab character? If not, do it.

Now output your age.

Was there a problem? What happened? Fix your output to decimal.

Go back to 2 and cause your output to show the base for each output.

Try reading as octal, hexadecimal, etc.:

cin >> a >>oct >> b >> hex >> c >> d;
cout << a << '\t'<< b << '\t'<< c << '\t'<< d << '\n';

Run this code with the input

1234 1234 1234 1234

Explain the results.

Write some code to print the number 1234567.89 three times, first using defaultfloat, then fixed, then scientific forms. Which output form presents the user with the most accurate representation? Explain why.

Make a simple table including last name, first name, telephone number, and email address for yourself and at least five of your friends. Use strings to hold all values, even for the phone numbers. Experiment with different field widths until you are satisfied that the table is well presented.

Defining a data type Point that has two coordinate members x and y. Define << and >> for Point as discussed in §9.3.1.

Using the code and discussion in §9.3.1, prompt the user to input seven (x,y) pairs. As the data is entered, store it in a vector<Point> called original_points.

Print the data in original_points to see what it looks like.

Open an ofstream and output each point to a file named mydata.txt. We suggest the .txt suffix to make it easier to look at the data with an ordinary text editor if you are using Windows.

Open an ifstream for mydata.txt. Read the data from mydata.txt and store it in a new vector called processed_points.

Print the data elements from both vectors.

Compare the two vectors and print “Something’s wrong!” if the number of elements or the values of elements differ.

Why is I/O tricky for a programmer?

What does the notation << hex do?

What are hexadecimal numbers used for in computer science? Why?

Name some of the options you may want to implement for formatting integer output.

What is a manipulator?

What is the default output format for floating-point values?

Explain what setprecision() and setw() do.

Which of the following manipulators do not “stick”: hex, scientific, setprecision(), setw()?

In format(), how do you specify where an argument is placed on output?

Give two examples where a stringstream can be useful.

When would you prefer line-oriented input to type-specific input?

What does isalnum(c) do?

When dealing with input and output, how is the variety of devices dealt with in most modern computers?

What, fundamentally, does an istream do?

What, fundamentally, does an ostream do?

What, fundamentally, is a file?

What is a file format?

Name four different types of devices that can require I/O for a program.

What are the four steps for reading a file?

What are the four steps for writing a file?

Name and define the four stream states.

Discuss how the following input problems can be resolved:

a. The user typing an out-of-range value
b. Getting no value (end-of-file)
c. The user typing something of the wrong type

In what way is input usually harder than output?

In what way is output usually harder than input?

Why do we (often) want to separate input and output from computation?

What are the two most common uses of the istream member function clear()?

What are the usual function declarations for << and >> for a user-defined type X?

How do you specify where an argument is inserted into a format string in format()?

What is the notation for bases of decimal values in format()?

How do you specify the precision of floating-point values in format()?

Write a program that reads a text file and converts its input to all lowercase, producing a new file.

Write a program that given a file name and a word will output each line that contains that word together with the line number. Hint: getline().

Write a program that removes all vowels from a file ("disemvowels"). For example, Once upon a time! becomes nc pn tm!. Surprisingly often, the result is still readable; try it on your friends.

Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base prefixes; interprets the numbers correctly; and converts them to decimal form. Then your program should output the values in properly spaced columns like this:

0x43    hexadecimal     converts to     67  decimal
0123    octal           converts to     83  decimal
65      decimal         converts to     65  decimal

Write a program that reads strings and for each string outputs the character classification of each character, as defined by the character classification functions presented in §9.10.3. Note that a character can have several classifications (e.g., x is both a letter and an alphanumeric).

Write a program that replaces punctuation with whitespace. Consider . (dot), ; (semicolon), , (comma), ? (question mark), (dash), (single quote) punctuation characters. Don’t modify characters within a pair of double quotes ("). For example, “− don’t use the as−if rule.” becomes “don t use the as if rule ”.

Modify the program from the previous exercise so that it replaces don’t with do not, can’t with cannot, etc.; leaves hyphens within words intact (so that we get “do not use the as−if rule ”); and converts all characters to lowercase.

Use the program from the previous exercise to make a sorted list of words. Run the result on a multi-page text file, look at the result, and see if you can improve the program to make a better list.

Write a function vector<string> split(const string& s) that returns a vector of whitespace-separated substrings from the argument s.

Write a function vector<string> split(const string& s, const string& w) that returns a vector of whitespace-separated substrings from the argument s, where whitespace is defined as “ordinary whitespace” plus the characters in w.

Reverse the order of characters in a text file. For example, asdfghjkl becomes lkjhgfdsa. Warning: There is no really good, portable, and efficient way of reading a file backward.

Reverse the order of words (defined as whitespace-separated strings) in a file. For example, Norwegian Blue parrot becomes parrot Blue Norwegian. Assume that all the strings from the file will fit into memory at once.

Write a program that reads a text file and writes out how many characters of each character classification (§9.10.3) are in the file.

Write a program that reads a file of whitespace-separated numbers and outputs a file of numbers using scientific format and precision 8 in four fields of 20 characters per line.

Write a program to read a file of whitespace-separated numbers and output them in order (lowest value first), one value per line. Write a value only once, and if it occurs more than once write the count of its occurrences on its line. For example, 75573 117 5 should give

3
5   3
7   2
117

Write a program that produces the sum of all the numbers in a file of whitespace-separated integers.

Write a program that creates a file of data in the form of the temperature Reading type defined in §9.3.2. For testing, fill the file with at least 50 “temperature readings.” Call this program store_temps.cpp and the file it creates raw_temps.txt.

Write a program that reads the data from raw_temps.txt created in exercise 2 into a vector and then calculates the mean and median temperatures in your data set. Call this program temp_stats.cpp.

Modify the store_temps.cpp program from exercise 2 to include a temperature suffix c for Celsius or f for Fahrenheit temperatures. Then modify the temp_stats.cpp program to test each temperature, converting the Celsius readings to Fahrenheit before putting them into the vector.

Write the function print_year() mentioned in §9.9.2.

Define a Roman_int class for holding Roman numerals (as ints) with a << and >>. Provide Roman_int with an as_int() member that returns the int value, so that if r is a Roman_int, we can write cout << "Roman" << r << " equals " << r.as_int() << ’\n’;.

Make a version of the calculator from Chapter 6 that accepts Roman numerals rather than the usual Arabic ones, for example, XXI + CIV == CXXV.

Write a program that accepts two file names and produces a new file that is the contents of the first file followed by the contents of the second; that is, the program concatenates the two files.

Write a program that takes two files containing sorted whitespace-separated words and merges them, preserving order.

Add a command from x to the calculator from Chapter 6 that makes it take input from a file x. Add a command to y to the calculator that makes it write its output (both standard output and error output) to file y. Write a collection of test cases based on ideas from §6.3 and use that to test the calculator. Discuss how you would use these commands for testing.

Write a program that produces the sum of all the whitespace-separated integers in a text file. For example, bears: 17 elephants 9 end should output 26.