i am working on an assignment where i have a file that has a list of numbers. using those numbers, i should be able to create a program, where when prompted, i can find the mean of the numbers from that file, the biggest numbers, etc.

the output would be like this:

Make a selection from the list

1. Get the largest value

2. Get the smallest value

3. Get the sum of the values

4. Get the average

5. Get the number of values entered

6. End this program

and then i could get that value from the file.

what i want to know, is how is a file attached to my sources for the programming and how i can get started on it. my book doesnt cover the topics much, nor does it cover what a menu-driven program is. (which i assume is this)

A menu drive program frequently has a list of options for the user to choose from, typically by having the user enter a letter or a number to correspond to the activity in the list they want to do. The user's input is then frequently used in a switch statement to have that task performed.

In C++ files are routinely handled using stream objects. An ifstream object can be used to read from a file. An ofstream object can be used to write to a file. An fstream object can be used to write or read to a file, depending on what mode it is in. You delcare a stream oject of the type desired. You can either pass a C style string (containing the desired file name) to the stream constructor or to the open() stream method to associate the desire file with the stream. Once the file is associated with the stream you can manipulate the material put into or taken out of the file using the same protocols you use with cin or cout (there are a few tricks to position the read/write location if you don't want/need to use the defaults, but that you can learn about later).

This article has been dead for over six months. Start a new discussion instead.