Thread: fstream problems

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002

    fstream problems

    I am trying to write a program that reads a line from a data that has an employee name and an employee salary on it. I need to bring that data from the file into the program, and assign it with functions from a .h file that my teacher gave us. I also need to calculate the average salary, the maximum salary and the minimum salary, plus count how many employees are created.

    I am having problems even getting started on this one. I have attached my program, and the "employee.cpp" and .h file that I am getting the functions from. There is a non-member function (I think) for pulling a name and salary from a stream, but I'm not sure how to call it or how to set it up to receive the stream data.

    Any suggestions?



  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Though I'll admit that "tutorial" is a little thin (someone really should expand on it), search google for "c++ fstreams"

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Here is a short program that will demonstrate the use of fstream and a bunch others. p.s. don't forget to add some numbers to a text file we'll call text1.dat then open another file called answer.dat which you will deliver your answer to.
    1st. upon input type text1.dat then answer.dat. next type type.out and the program should display processing complete. open your answer text file and presto! You answer has been copied to the answer.dat file
    If you have anymore ?. pls ask me and I will try to help as much as possible.

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    #include <cfloat>
    using namespace std;
    int main()
    	cout << "This program computes the number, maximum, minimum, and\n"
    			"average of an input list of numbers in one file,\n"
    			"and places its results in another file.\n\n";
    	// input section
    	cout << "Enter the name of the input file: ";
    	string inputFileName;
    	getline(cin, inputFileName);
    	ifstream inStream;;
    	int count = 0;
    	double reading,
    		maximum = DBL_MIN,
    		minimum = DBL_MAX,
    		sum = 0.0;
    		inStream >> reading;
    		if(inStream.eof()) break;
    		sum += reading;
    		if (reading < minimum)
    			minimum = reading;
    		if (reading > maximum)
    			maximum = reading;
    	// output section
    while(getchar() != '\n');
      cout << "Enter the name of the oputput file: ";
    	string outputFileName;
    	getline(cin, outputFileName);
    	ofstream outStream(;
    	outStream << "\n--> There were " << count << " values.";
    	if (count > 0)
    		outStream << "\n\tranging from " << minimum
    				  << " to " << maximum
    				  << "\n\tand their average is " << sum / count
    				  << endl;
    	cout << "Processing complete.\n";
    	return 0;
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd fstream problems...
    By Jaken Veina in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 04:44 PM
  2. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  3. more fstream problems
    By howzer in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 06:17 AM
  4. unusual fstream problems
    By georgefoxjunior in forum C++ Programming
    Replies: 11
    Last Post: 09-09-2004, 08:21 AM
  5. I/O Problems - fstream
    By mol in forum C++ Programming
    Replies: 17
    Last Post: 10-16-2001, 03:50 PM