fstream problems

This is a discussion on fstream problems within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that reads a line from a data that has an employee name and ...

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

    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?

    Thanks

    Brian
    Attached Files Attached Files

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    http://www.cprogramming.com/tutorial/lesson10.html
    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
    Posts
    378
    Greetings,
    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.


    cj
    Code:
    #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;
    		inStream.open(inputFileName.data());
    	assert(inStream.is_open());
    	int count = 0;
    	double reading,
    		maximum = DBL_MIN,
    		minimum = DBL_MAX,
    		sum = 0.0;
    
    	for(;;)
    	{
    		inStream >> reading;
    
    		if(inStream.eof()) break;
    
    		count++;
    		sum += reading;
    		if (reading < minimum)
    			minimum = reading;
    		if (reading > maximum)
    			maximum = reading;
    	}
    	inStream.close();
    
    	// output section
    
    while(getchar() != '\n');
    
      cout << "Enter the name of the oputput file: ";
    	string outputFileName;
    	getline(cin, outputFileName);
    
    	ofstream outStream(outputFileName.data());
    	assert(outStream.is_open());
    	outStream << "\n--> There were " << count << " values.";
    
    	if (count > 0)
    		outStream << "\n\tranging from " << minimum
    				  << " to " << maximum
    				  << "\n\tand their average is " << sum / count
    				  << endl;
    	outStream.close();
    	cout << "Processing complete.\n";
    	return 0;
    }
    Enjoy!
    "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, 05: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, 09:21 AM
  5. I/O Problems - fstream
    By mol in forum C++ Programming
    Replies: 17
    Last Post: 10-16-2001, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21