-
1 Attachment(s)
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
-
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"
-
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!