Thread: Lots of Questions about my payroll program..

  1. #1
    Registered User
    Join Date
    Apr 2010

    Lots of Questions about my payroll program..

    hello, i previously posted here asking about print to screen issue, i got help and i thought it worked cuz i run it and it worked flawlessly and then now i add some stuff to it and now everything broken and when i revert it, its still broken so it seems like it wasnt fixed at all...

    here's the problem, i add records to a text file like
    name id gross net pay
    john 102 2300 1980

    and then now i use a diff function to locate a single record based on user input to change a certain record using fseek. the file pointer is giving me problems in overwriting the correct record. here's the code

    void edit_record(FILE *rptr)
    	int editchoice=1;
    	FILE *writeptr;
    	struct employee_data employee;
    	if ((writeptr=fopen("Payroll.txt", "r+")) == NULL){
    		printf("file cannot be opened\n");
    	while (editchoice== 1) {
    		printf("Enter record number to be editted\n");
    		scanf("%d", &employee.num);
                              //GETTING DATA FROM FILE TO PRINT TO SCREEN
    		fseek(writeptr, (employee.num -1)*sizeof(struct employee_data), SEEK_CUR);
    		fscanf(writeptr, "%d %s %s %d %f %f %f %f %f %f %f" , &employee.num, employee.first_name, employee.last_name, &employee.id_number, &employee.pay_rate, 
    				&employee.sthours, &employee.one_and_half_hour, &employee.double_hour, &employee.gross_pay, &employee.deductions, &employee.net_pay);
    		 printf("%-15s %-15s %-12s %-14s %-12s\r\n", "First name", "Last name", "Gross pay", "deductions", "net Pay");
    		printf("%-15s %-15s %-12.2f %-14.2f %-12.2f\n", employee.first_name, employee.last_name, &employee.gross_pay, &employee.deductions, &employee.net_pay);
    		if (employee.num == 0){ 
    			printf("record number %d has no info\n", employee.num);
    		}// end if
    		printf("adding a new record, option 1\n");
    		printf("Enter first name\n");
    		scanf("%s", &employee.first_name);
    		printf("Enter last name\n");
    		scanf("%s", &employee.last_name);
    		printf("Enter Employee's ID number\n");
    		scanf("%d", &employee.id_number);
    		printf("Enter Pay Rate\n");
    		scanf("%f", &employee.pay_rate);					
    		printf("Enter Straight time hours\n");
    		scanf("%f", &employee.sthours);			
    		printf("Enter One and Half time Hours\n");
    		scanf("%f", &employee.one_and_half_hour);			
    		printf("Enter double time hours\n");
    		scanf("%f", &employee.double_hour);
    		employee.gross_pay = employee.pay_rate*employee.sthours + employee.pay_rate*1.5*employee.one_and_half_hour + employee.pay_rate*2*employee.double_hour;
    		employee.nis_tax = employee.gross_pay * 0.025;
    		employee.gross_not_nis = employee.gross_pay - employee.nis_tax;	
    		employee.nht_tax = employee.gross_pay * 0.02;   			   
    		employee.edu_tax = employee.gross_pay * 0.02;   			  
    		employee.heart_tax = employee.gross_pay * 0.03; 				
    		employee.Income_tax = (employee.gross_pay-employee.nis_tax) - 8484;	
    		 if (employee.gross_not_nis>= 8484){ 
    		   employee.Income_tax= ((employee.gross_pay-employee.nis_tax - 8484)*0.25)+(employee.gross_pay-employee.nis_tax) - 8484;	 
    		} //end if
    		if (employee.gross_not_nis < 8484){
    		} //end if
    		employee.deductions=employee.nis_tax + employee.nht_tax + employee.edu_tax + employee.heart_tax + employee.Income_tax;
    		employee.net_pay=employee.gross_pay - employee.deductions;
    		employee.num =employee.num+1;
    		fseek(writeptr, (employee.num-1) * sizeof(struct employee_data), SEEK_CUR);
    		fprintf(writeptr, "%-10d%-15s%-15s%-12.2f%-8d%-12.2f%-12.2f%-12.2f%-12.2f%-14.2f%-12.2f\r\n" , employee.num, employee.first_name, employee.last_name, employee.id_number, employee.pay_rate, employee.sthours, employee.one_and_half_hour, employee.double_hour, employee.gross_pay, employee.deductions, employee.net_pay);
    		printf("do you want to edit another file? press 1 to do so or 0 to exit");
    		scanf("%d", &editchoice);
    	} //end while
    }//end fuction
    And also i can make a record then load it and it will print the correct record on the screen but if i just open the program and try to edit a record that was previously made it will show garbage. i know this is due to the struct not containing the correct info, how can i fix this?

    when i do get it to work, it overwrites the first one correctly but as more records are made it starts overwritng wrong like start overwriting at the second column and leave the first untouched with the original data and continue deviating more as the records increase. I want to know how to fix this as well.

    thanks in advance, i know i am asking alot outta you guys sorry....

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You appear to write 134 characters for every line. Is sizeof(struct employee_data) equal to 134? (Hint: no.) Also, since those field widths are minimum, if you have a long name/large number, then you will write a longer line and then you're just sunk. If you know that won't happen, then you need to use the size of the lines themselves as they are printed, rather than as they are stored in memory.

  3. #3
    Registered User
    Join Date
    Apr 2010
    wow, didnt realize it would be such a simple answer... i tried it and it worked perfect for the writing to the file correctly replacing the records but had some problems getting the printing to the screen one working. I removed the printing to the screen for now will try fooling around with it to get it working. Thanks for the help! you saved me alot of stress and pain

    And about the large number/large name issue, i dont need something like that so i think i am fine but i am curious, how would you deal with a problem like that?

  4. #4
    Registered User
    Join Date
    Sep 2006
    If these fields are strings, why do have an ampersand in the scanf() lines of code, for them?

    		scanf("%s", &employee.first_name);
    		printf("Enter last name\n");
    		scanf("%s", &employee.last_name);

  5. #5
    Registered User
    Join Date
    Apr 2010
    ah thanks, i didnt realize the ampersand was there. I fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a payroll program
    By xc-racer in forum C++ Programming
    Replies: 10
    Last Post: 02-22-2004, 11:31 PM
  2. Lots Of Bitmap Texture Questions:
    By Krak in forum Game Programming
    Replies: 7
    Last Post: 07-10-2003, 08:16 PM
  3. payroll program with loops
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2002, 09:49 PM