473,503 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Functions using in/outfile

12 New Member
Hello,
Im having really hard time writing an Employee Payroll program that uses functions to read data from file then send all data to another file. I managed to construct some pieces of the code, but I cant figure out how to put it together. Here's the description of whats needed to be done:

Write a program that generates an Employee Payroll. All output should go to payroll.out. The input for the program should be read from a file, payroll.data. Payroll.data should contain the following information for 5 employees:

id hours rate
1 5.0 15.75
2 52.5 17.50
3 40.0 14.00
4 50.0 14.00
5 5.25 12.25
The first column contains the number of the employee, the second column is the number of hours worked, and the third is the hourly pay rate.

Your program should perform the following tasks:

First call a function printPersonalInfo() which will not receive any parameters and will not return a value. This function will print your name and a description of the program to the output file, payroll.out.

For each employee you are to read in their id, hours, and pay rate from the input file, payroll.data and print them out to the output file.

Next you should call a function calculateEmployeeData() and pass in two parameters: hours and pay rate. Your function calculateEmployeeData() should compute and print to the output file the number of overtime hours worked by that employee, their overtime pay, and their total salary. An employee earns straight time for the first forty hours of work and double-time for any additional hours. For example if an employee worked 45 hours earning $10 and hour, then the employee's overtime hours would be 5, their overtime pay would be $100.00 and their total pay would be $500.00. The function should return the total salary.

Then, your main program should call a function, checkHours() passing in hours to the function. The function checkHours() prints out the following messages to the output file (You must use nested if-else statments (i.e. USE else if) :


If hours is less than 10 print a message saying that the employee has worked too little.
If hours is between 10 and 20, print a message saying the employee should work more than 20 hours a week.
If hours is between 20 and 40 it should print a message thanking the employee for putting in the time.
Otherwise (greater than 40 hours), it should print a very nice message letting the employee know that s/he has gone above and beyond what is required.

After there are no more employees to process, the program should print out to the output file the total number of hours and total salary for all employees combined.

I entered all the required data into infile "payroll.data" and it successfully reads from it and sends to outfile "payroll.out". However, after I send original info to file and then try to add my name and discription, as well as employee salaries etc.... (which must be separate from original data in outfile) it only mixes everything up, overrides it, or just shows results for the last employee in the for loop instead of all 5. I'm not even sure what to do in main. Please help me modify this code to work and put it together. Here's my code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. void printPersonalInfo();
  6. double calculateEmployeeData(int hours, int payrate); 
  7. void checkHours();
  8.  
  9.  
  10. double calculateEmployeeData(int hours, int payrate){
  11.     int id;
  12.     double totalsalary, extrahours = 0, overtimepay = 0, overtime = 0;
  13.     double hours, payrate;
  14.     ifstream infile;
  15.     infile.open("payroll.data");
  16.     ofstream outfile;
  17.     outfile.open("payroll.out", ofstream::out);
  18.     outfile.setf(ios::fixed,ios::floatfield);
  19.     outfile.precision(2);
  20.     for (int i = 1; i<= 5; i++){
  21.         infile >> id;
  22.         infile >> hours;
  23.         infile >> payrate;
  24.         outfile << id << "  ";
  25.         outfile << hours << "  ";
  26.         outfile << payrate << "  \n\n";
  27.         }
  28.         for (int i = 1; i<= 5; i++){
  29.         infile >> id;
  30.         infile >> hours;
  31.         infile >> payrate;
  32.         if (hours > 40){
  33.         extrahours = hours - 40;
  34.         }
  35.         if (hours > 40){
  36.            overtimepay = payrate * 2;
  37.            }
  38.            if (hours > 40){
  39.            overtime = extrahours * overtimepay;
  40.            }
  41.            totalsalary = (hours - extrahours) * payrate + overtime;
  42.            outfile << "id: " << id << "  ";
  43.            outfile << "extrahours: " << extrahours << "  ";
  44.            outfile << "overtimepay: " << overtimepay << "  ";
  45.            outfile << "totalsalary: " << totalsalary << endl;
  46.     }
  47.     return totalsalary;
  48. }
  49. void printPersonalInfo(){
  50.     cout<<"name"<<endl;
  51.     cout<<"description"<<endl;
  52. }
  53.  
  54.  
  55. void checkHours(){
  56. if (hours <= 10)
  57.   outfile << "you worked too little";
  58. else if (hours > 10 && hours <= 20)
  59.   outfile << "Please work more than this";
  60. else if (hours > 20 && hours <= 40)
  61.   outfile << "You wroked suficient amount of time";
  62. else
  63.   outfile << "Thank you for working overtime";
  64. }

Any help is very much appreciated. Thank you in advance.
Nov 9 '06 #1
2 5203
Banfa
9,065 Recognized Expert Moderator Expert
the mistake is in

double calculateEmployeeData(int hours, int payrate){

it attempts to read the file twice (2 for loops) but once the file has been read once the file pointer it is at the end of the file so it can't be read again.

Additionally according to the problem statement calculateEmployeeData should not be reading the input file at all, main should be reading the input file and for each line read it should call calculateEmployeeData with the number of hours and pay rate, calculateEmployeeData then uses these variables to write data to the output file. The question notably fails to transfer the employee ID to calculateEmployeeData or request that it is output with the data which is a little strange.

checkhours needs a parameter (the number of hours worked)
Nov 9 '06 #2
varusnyc
12 New Member
I think I understand what you mean. But Im still confused on how to actually work with the code to make it work like that. Could you please show me an example with piece of my main, call my functions with proper parameters assigned to them and how will it actually be structured inside main for everything to go into outfile without overriding anything, but rather adding information. Thanks.
Nov 9 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2156
by: Ebi | last post by:
It's a main function of club program in Borland c++ 5; There is a film class in my club program... But I have a problem with it: whenever I add a film by addfilm function to film.dat file,...
4
1222
by: David. E. Goble | last post by:
#include <stdio.h> #include <string.h> #define FALSE 0 #define TRUE 1 #define LINESIZE 255 int setupoutfile(char filename, FILE *outfile) { strcat(filename, ".js");
0
1194
by: tim | last post by:
I wrote this very simple program that checks a directory and makes a list of files that surpass a given size. I tried to compile it using py2exe. I used py2exe before with more complex programs...
12
1953
by: Felix85 | last post by:
here is my method for reading in a file: static room room::file2Room(int rnum){ ostringstream filename; filename << "../gamefiles/rooms/" << rnum << ".room"; ifstream...
3
2629
by: MariyaGel | last post by:
I have wrote the program and it worked fine until I had to include one more array into it. As you can see below the two arrays of volume and mass are working properly now I need to include a third...
1
1360
by: southparkcab | last post by:
# include <stdio.h> # include <stdlib.h> # include <math.h> # define M_SQRT /* macro to insert blank lines on screen */ #define BLANK_LINE(n) {int i=0;for(i<(n);i++)fprintf(stderr,"\n";} ...
3
8210
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
4
1691
by: Trent | last post by:
Still have problems with this thing. Seems my results are not matching the "correct" example given. The three sets of numbers below the last 3 columns is suppose to be the number of comparisons...
19
2862
Sheepman
by: Sheepman | last post by:
I still messin' around with functions. This is a variant of the program I was messing up early. I'm trying to reduce the amount of work each function is doing. I figured if could get this to work I...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.