CS 102 Lab #6

This lab has two parts:

Part 1

Write a program that prompts for the name of a file containing a bunch of integers, and reads the contents (all integers) into a list of integers. When the file is loaded, the program should repeatedly prompt the user for a number, and if the number is on the list output "yes", if not it should output "no". When the user types the number 0, the program should exit.

For example, if the file "data" contained: 1 7 3 5 7 9 10:

Enter file name: data

Enter number: 1
Yes
Enter number: 2
No
Enter number: 0
Bye!

Part 2

Modify your program from part 1 to output the elements of the list beginning with the (first occurrence of the) number entered. For example:

Enter file name: data
Enter number: 1
1 7 3 5 7 9 10
Enter number: 2
no
Enter number: 7
7 3 5 7 9 10
Enter number: 10
10
Enter number: 0
Bye!

Hint: Write a function called "find" that takes a list of integers and an integer as parameters and returns an iterator from the list that references the element found, or references the end() iterator if not found. Note that if you take this approach, you must declare the list of integers parameter to be a reference parameter. We will discuss why this is in class next week. The interface for find would be:

typedef list<int>::const_iterator intItor;

intItor find(list<int>& l, int num);