Skip to main content

Exercises 11.12 Exercises

The exercises in this chapter make use of the following datafiles.
Listing 11.12.1.
Listing 11.12.2.

1.

Write a program that reads the first skipWords number of words from poem.txt without printing them and prints the next word to cout.
Hint 1.
You might want to start by reading and printing the first word just to make sure your input is working.
Hint 2.
While debugging, print out something like WORD skip for each skipped word. Once you are confident you are skipping the words, you can remove that print statement.

2.

Write a program that reads the first skipLines number of lines from poem.txt without printing them and prints the next line to cout.
Hint.
getline is the easy way to read a whole line.

3.

The file class.txt has lines that look like this:
First      Last     Grade        GPA    Age
Hidden code will open the file and read past some number of the lines (determined by the input). You should add code to continue reading from inFile and print the first name from the next line in the file. (It is safe to use >> for your input even though the hidden code uses getline).
Hint 1.
This is much easier using >> instead of getline
Hint 2.
Remember you have to read all of the tokens in order. If you don’t want a piece of data, read it and then do nothing with it.

4.

The file class.txt has lines that look like this:
First      Last     Grade        GPA    Age
Hidden code will open the file and read past some number of the lines (determined by the input). You should add code to continue reading from inFile and print the first name and age (with a space between) of the next line in the file. (It is safe to use >> for your input even though the hidden code uses getline).
Hint 1.
This is much easier using >> instead of getline
Hint 2.
Remember you have to read all of the tokens in order. If you don’t want a piece of data, read it and then do nothing with it.

5.

The file class.txt has lines that look like this:
First      Last     Grade        GPA    Age
Hidden code will open the file and read past some number of the lines (determined by the input). You should add code to continue reading from inFile and print out the next three first names with a space after each. (It is safe to use >> for your input even though the hidden code uses getline).
Hint 1.
If you use >>, remember that you have to read all of the tokens in order. If you don’t want a piece of data, read it and then do nothing with it.
Hint 2.
This is a spot where it might make sense to mix >> and getline. On each line, consume the first name by reading it into a variable, then use getline to read the rest of the line (and ignore it).

6.

The file class.txt has lines that look like this:
First      Last     Grade        GPA    Age
Hidden code will open the file and read past some number of the lines. You should add code to continue reading from inFile and print out the average of the GPA values for all the rest of the lines. (It is safe to use >> for your input even though the hidden code uses getline).
Note that there is a newline after the last piece of data (the line that starts with Gigi and ends with 14). You will likely need to check infile for an error after attempting to read in the data for each line before you count the line or do anything with it.
Hint 1.
Start by just printing out all of the GPAs. The last two GPAs should be 3 and 3.2. If you see an extra value after those, or two copies of 3.2 at the end, you are probably trying to read one extra line of data and failing. Check for failure after reading the data before you use it.
Hint 2.
You will need to accumulator variables - one to add up the GPAs and the other to keep a count of how many lines you have read.
You have attempted of activities on this page.