Skip to main content

Section 1.5 Homework 5: Input/Output

The goal of this assignment is to give you some practiceΒ working with C++ file I/O and to check your knowledge of some of the main concepts on this topic.

Exercises Exercises

1.

What is the name of the header file that you need to include if you want your C++ program to read from, or write to, a file?
  • fstream
  • iostream
  • stringstream
  • stdio

2.

Which of the following can be used as input streams?
  • cout
  • cin
  • an object of type ifstream
  • an object of type ofstream

3.

4.

Which of the following statements about the newline character ’\n’ and the endl object are true?
  • When used at the end of writing to an output stream like `cout`, both ’\n’ and endl have the potential to leave data in the output buffer if the program crashes.
  • Both cout << '\n' and cout << endl will write an end-of-line (a newline character) to standard output.
  • The difference between ’\n’ and endl is that endl will automatically flush the output buffer.

5.

Which one of the following when executed will look ahead and return the next character in the cin input stream without actually reading the character?
  • cin.ignore()
  • cin.peek()
  • cin.get(c)
  • cin >> c

6.

Which one of the following will cause a failure in the standard input stream (cin)?
  • Using chaining to read in several different values, one right after the other. For example: cin >> a >> b >> c;
  • Reading in illegal data for an input operation.Β  An example of this would be if the variable ’num’ is an int, and when cin >> num is executed, the user enters a string instead of an integer.
  • Using ’cin’ as the input stream argument for the getline function. For example, getline(cin, message) where ’message’ is a string.

7.

8.

Write a complete C++ program that will open a file called ’mystrings.txt’, and write to standard output thefirst character from each line in the file, with all output on the same line.
For example, if mystrings.txt contains the following data:
happy smile laugh
sad frown cry
angsty hyper
euphoric delightful wonderful lovely
maudlin
your program would output:
hsaem
You may assume that mystrings.txt does not contain any blank lines.
HINT: Use while(getline(<your args here>)) to loop over the lines of a file.

9.

The first line of the file ’calculate.txt’ contains an integer. The following lines contain simple arithmetic expressions, one per line. The integer on the first line specifies how many calculations will follow.
  • All calculations will contain numbers with decimal points; use variables of type `double` for reading the numbers in the expressions.
  • The math operators your program must handle are: +, -, * , /
Below is a sample of what the file calculate.txt might look like:
5
11.1 + 3.2
-2.0 - 8.0
3.0 * 12.3
9.0 / 3.010.0 / 9.0
Write a complete C++ program that will write the results of the calculations (one per line) to standard output WITH A PRECISION OF 3 (three significant figures). For example, if calculate.txt contains the data above, your program would output the following:
14.3
-10
36.9
31.11
HINT: Because the first line of the file tells you exactly how many more lines need to be read, and each line after the first contains 3 tokens in the format <double> <char> <double>, it’s easier to read the arithmetic expressions using the input stream operator `>>` (also called the "stream extraction operator") rather than `getline`.

10.

For this problem, you are working with a text file, "cat_treats.txt".Β  Each line of the file contains the name of one of Dr. Wilson’s cats (Cleo, Skye, Spike, or Ziggy) followed by one or more spaces and then an integer such as 2 (representing the gain of 2 treats for that cat) or -3 (representing the loss of 3 treats for that cat), except that the last line of the file has just the word "END".
Note: Some lines might start with a cat name which is not one of Dr. Wilson’s cats.Β  In that case, your program should print an error message.Β  See the example below.
To help Dr. Wilson’s cats keep track of their cat treat stashes, write a complete C++ program to read the text file and print a corresponding report.Β For each line in the input file (except the "END" line), your program needs to print the cat’s name and the current total number of treats for that cat.
Assume that all cats start with zero treats.
Below is a sample of what the file cat_treats.txt might look like:
Ziggy 1
CleoΒ  4
Ziggy 5
CleoΒ  Β  7
Scar 28
Ziggy -3
Ziggy -2
Skye 105
Spike 16
Mr.Grey 1000
END
If the input file was exactly as shown above, then your program should produce the following output:
Ziggy has 1
Cleo has 4
Ziggy has 6
Cleo has 11
Hey, Scar is not one of Dr. Wilson's cats!
Ziggy has 3
Ziggy has 1
Cleo has 12
Skye has 105
Spike has 16
Hey, Mr.Grey is not one of Dr. Wilson's cats!
You have attempted of activities on this page.