954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

input/output file

hi.

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)

afg_91320
Junior Poster in Training
55 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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).

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

In other words, look at the chapter on Input and Output

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You