Thread: Need Help Fast Please

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Need Help Fast Please

    I have a piece of code that I had to work on for a class assignment. I'm trying to figure out where all of my mistakes are and i narrowed it down pretty nicely until i started getting a batch of other problems out of nowhere, take a look and give me feedback if you would. THANK YOU!!!

    Write a program to read the employee file and create a payroll register. The register will contain the following data:

    A. Employee number (print left justified)
    B. Department
    C. Pay rate
    D. Exempt
    E. Hours worked
    F. Base pay (pay rate * hours worked)

    the employee file is just a .txt file im supposed to create with this information:

    Employee No. Department Pay rate Exempt Hours worked
    0101 41 8.11 Y 49
    0722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45

    Code:
    /* Creates a Payroll Register from an Employee File Saved on the Computer
    	Written by: Vincent Gorski
    	Date: 11/5/10
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    //Function Declarations
    	int getEmpData		(FILE* spEmpDataIn,
    						 int* empNum, int* dept, float* rate, char* exempt, int* hrs);
    	int writeEmpData    (FILE* spEmpDataOut, 
    						 int empNum,  int dept,  float rate,  char exempt,  int hrs, 
    						 float basePay);
        void calcPay	    (float rate,  int hrs,  float* basePay);	
    
    int main (void)
    {
    
    //Local Declarations
    	FILE* spEmpDataIn;
    	FILE* spEmpDataOut;
    	
    	int	  empNum;
    	int   dept;
    	float rate;
    	char  exempt;
    	int   hrs;
    
    	float basePay;
    
    //Statements
    	printf("Creating Payroll Register...\n");
    	if (!(spEmpDataIn = fopen ("C:\\Employee.txt", "r")))
    		{
    		 printf("Could not open file\a\n");
    		 return 100;
    		} //if open input
    	
    	if (!(spEmpDataOut = fopen ("C:\\PayrollRegister.txt", "w")))
    		{
    		 printf("Error Opening Payroll Register\a\n");
    		 return 102;
    		} //if open output
    
    	while (getEmpData
    		  (spEmpDataIn, &empNum, &dept, &rate, &exempt, &hrs))
    		{
    		 calcPay	  (rate, hrs, &basePay);
    		 writeEmpData (spEmpDataOut, empNum, dept, rate, exempt, hrs, basePay);
    		} //while
    
    	fclose (spEmpDataIn);
    	fclose (spEmpDataOut);
    
    	printf("Payroll Register Completed\n");
    	return 0;
    } //main
    
    /*=====================getEmpData=====================
    	Reads data from the employee file.
    	   Pre	 spEmpDataIn is an open file
    			 empNum, dept, rate, exempt, hrs pass by address
    	   Post	 reads employee information
    			 if data read	--returns 1
    			 if EOF or error--returns 0
    */
    int getEmpData (FILE* spEmpDataIn, int* empNum,  int* dept, 
    				float* rate,	   char* exempt, int* hrs)
    {
    //Local Declarations
    	int ioResult;
    
    //Statements
    	ioResult = fscanf(spEmpDataIn, "%4d%2d%4f%1c%2d", &empNum, &dept, &rate, &exempt, &hrs);
    	if(ioResult == EOF)
    		return 0;
    	else
    		return 1;
    } //getEmpData
    
    /*===========================calcPay===========================
    	Determines base pay based on the employees pay rate and hours worked.
    		Pre		rate and hrs contain employee pay rate and hours worked
    				basePay is the adress of the variable
    		Post	Base Pay is copied to the address
    */
    void calcPay (float rate, int hrs, float* basePay)
    {
    //Statements
    	*basePay = rate * hrs;
    	return;
    } //calcPay
    
    /* =======================writeEmpData=======================
    	Write payroll register to output file.
    		Pre		spEmpDataOut is an open file
    				empNum,dpt, rate, exempt, hrs, and basePay have values to write
    		Post	Data written to file
    */
    int writeEmpData (FILE* spEmpDataOut, int empNum,  int dept, 
    				  float rate,		  char exempt, int hrs, 
    				  float basePay)
    {
    //Statements
    	fprintf(spEmpDataOut, "%-4d %d %f %c %d %f\n",
    						   empNum, dept, rate, exempt, hrs, basePay);
    	return 0;
    } //writeEmpData

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    take a look at this...
    i think that the problem was that you wanted to pass arguments by reference and not by value. So, you were correct in main and in the functions arguments but when using these arguments inside the function we always use *...hope i helped!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getEmpData (FILE* spEmpDataIn, int* empNum,  int* dept, float* rate,	   char* exempt, int* hrs){
    	int ioResult;
    		ioResult = fscanf(spEmpDataIn,"%4d%2d%4f%1c%2d", &(*empNum), &(*dept), &(*rate), &(*exempt), &(*hrs));
    		if(ioResult == EOF){
    			return 0;
    		}else{
    			return 1;
    		}
    } 
    
    void calcPay (float rate, int hrs, float* basePay){
    	*basePay = rate * hrs;
    	return;
    } 
    
    int writeEmpData (FILE* spEmpDataOut, int empNum,  int dept, float rate, char exempt, int hrs, float basePay){
    	fprintf(spEmpDataOut, "%-4d %d %f %c %d %f\n", empNum, dept, rate, exempt, hrs, basePay);
    	return 0;
    }
    
    int main (void){
    	FILE* spEmpDataIn;
    	FILE* spEmpDataOut;
    	int	  empNum;
    	int   dept;
    	float rate;
    	char  exempt;
    	int   hrs;
    	float basePay;
    
    	printf("Creating Payroll Register...\n");
    	if (!(spEmpDataIn = fopen ("C:\\Employee.txt", "r")))
    		{
    		 printf("Could not open file\a\n");
    		 return 100;
    		} //if open input
    	
    	if (!(spEmpDataOut = fopen ("C:\\PayrollRegister.txt", "w")))
    		{
    		 printf("Error Opening Payroll Register\a\n");
    		 return 102;
    		} //if open output
    
    	while (getEmpData
    		  (spEmpDataIn, &empNum, &dept, &rate, &exempt, &hrs))
    		{
    		 calcPay	  (rate, hrs, &basePay);
    		 writeEmpData (spEmpDataOut, empNum, dept, rate, exempt, hrs, basePay);
    		} //while
    
    	fclose (spEmpDataIn);
    	fclose (spEmpDataOut);
    
    	printf("Payroll Register Completed\n");
    	return 0;
    }
    is it ok now?
    Last edited by brack; 11-08-2010 at 02:22 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Talking Thank you!

    Weeded out all the problems! apparently on my fscanf i also needed to put spaces in between the conversion specifications (ie. %4d, %2d, etc). Thank you for your help though, that was my main problem that you brought to my attention.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > &(*empNum)
    You do know that this is just
    empNum

    Dereferencing (*) a pointer, then taking the address (&) of the result gets you back to where you started.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Lightbulb wow

    guess i was a little blind in my desperate time of need, lol, i didnt even notice that until now. thx for pointing that out man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-27-2009, 04:43 PM
  2. Saving a part of screen into a file fast!
    By Zap in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2003, 10:56 AM
  3. Super fast bilinear interpolation
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 06-18-2002, 09:35 PM
  4. Fast clrscr function
    By GaPe in forum C Programming
    Replies: 24
    Last Post: 06-13-2002, 12:54 AM
  5. moving a bitmap, fast and smooth
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 05-31-2002, 06:49 PM