Information overwritten in linked list

This is a discussion on Information overwritten in linked list within the C Programming forums, part of the General Programming Boards category; Code: void calAllPay(struct PersonalData* user){ struct PersonalData* currentEmp = user; int rate = 0; struct CompData* calculatedData; calculatedData = malloc(sizeof(struct ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24

    Information overwritten in linked list

    Code:
    void calAllPay(struct PersonalData* user){
    
    	struct PersonalData* currentEmp = user;
    	int rate = 0;
    	struct CompData* calculatedData;
    	calculatedData = malloc(sizeof(struct CompData));
    
    	while(currentEmp!=NULL){
    		calculatedData->otPay = 0;
    		calculatedData->weeklyPay = 0;
    
    		if (currentEmp->hours > 40){
    			calculatedData->otHours = currentEmp->hours - 40;
    
    			switch(currentEmp->jobCat){
    				case 'a': case 'A':
    					rate = 10;
    					break;
    				case 'b': case 'B':
    					rate = 15;
    					break;
    				case 'c': case 'C':
    					rate = 20;
    					break;
    			}
    			if (calculatedData->otHours <= 5){
    				calculatedData->otPay += (rate * 1.5);
    			}
    			else if ((calculatedData->otHours > 5) && (calculatedData->otHours <= 10)){
    				calculatedData->otPay += (rate * 1.5);
    				calculatedData->otPay += (rate * 1.65);
    			}
    			else if ((calculatedData->otHours > 10) && (calculatedData->otHours <= 15)){
    				calculatedData->otPay += (rate * 1.5);
    				calculatedData->otPay += (rate * 1.65);
    				calculatedData->otPay += (rate * 1.75);
    			}
    			
    			else if (calculatedData->otHours > 15){
    				calculatedData->otPay += (rate * 1.5);
    				calculatedData->otPay += (rate * 1.65);
    				calculatedData->otPay += (rate * 1.75);
    				calculatedData->otPay += (rate * 1.6);
    			}
    
    			calculatedData->weeklyPay = currentEmp->basicPay + calculatedData->otPay;
    		}
    
    		else
    		{
    			calculatedData->otHours = 0;
    			calculatedData->otPay = 0.00;
    			calculatedData->weeklyPay = currentEmp->basicPay;
    		}
    
    		currentEmp->dataElement = calculatedData;
    		currentEmp = currentEmp->nextElement;
    		
    
    	}
    	&*user = currentEmp;
    	printf("Pay successfully computed\n");
    }
    What i am supposed to achieved is calculated the pay for all employees, however, in my code, all the dataElement in the link list becomes the same , as the last element

    How can i stop it from overwriting

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > calculatedData = malloc(sizeof(struct CompData));
    You need to move this down into the while() loop. Otherwise all currentEmp->dataElement's will have the same allocated address.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24
    oh thanks, i got another problem that i do not understand... i supposed its another address issue..

    Code1
    Code:
    void calSinglePay(int empNo, struct PersonalData* user){
    	
    	struct PersonalData* currentEmp;
    	struct PersonalData* empData;
    	int exist = 0; 
    	int rate = 0;
    
    	struct CompData calculatedData;
    	calculatedData->otPay = 0;
    	calculatedData->weeklyPay = 0;
    
    	for (currentEmp=&*user; currentEmp!=NULL; currentEmp=currentEmp->nextElement){
    		if (currentEmp->code == empNo){
    			exist = 1;
    			empData = currentEmp;
    			break;
    		}
    		else
    			exist = 0;
    	}
    	if (exist = 1){
    		if (empData->hours > 40){
    			calculatedData->otHours = empData->hours - 40;
    
    			switch(empData->jobCat){
    				case 'a': case 'A':
    					rate = 10;
    					break;
    				case 'b': case 'B':
    					rate = 15;
    					break;
    				case 'c': case 'C':
    					rate = 20;
    					break;
    			}
    			if (calculatedData->otHours <= 5){
    				calculatedData->otPay += (rate * 1.5);
    			}
    			else if ((calculatedData->otHours > 5) && (calculatedData->otHours <= 10)){
    				calculatedData->otPay += (rate * 1.5);
    				calculatedData->otPay += (rate * 1.65);
    			}
    			else if ((calculatedData->otHours > 10) && (calculatedData->otHours <= 15)){
    				calculatedData->otPay += (rate * 1.5);
    				calculatedData->otPay += (rate * 1.65);
    				calculatedData->otPay += (rate * 1.75);
    			}
    			
    			else if (calculatedData->otHours > 15){
    				calculatedData->otPay += (rate * 1.5);
    				calculatedData->otPay += (rate * 1.65);
    				calculatedData->otPay += (rate * 1.75);
    				calculatedData->otPay += (rate * 1.6);
    			}
    
    			calculatedData->weeklyPay = empData->basicPay + calculatedData->otPay;
    		}
    
    		else
    		{
    			calculatedData->otHours = 0;
    			calculatedData->otPay = 0.00;
    			calculatedData->weeklyPay = empData->basicPay;
    
    		}
    		//Only can calculate 1 data, 2nd calculation corrupted	
    		&*user->dataElement = calculatedData; 
    		printf("Pay successfully computed\n");
    
    	}
    
    	else{
    		printf("Employee does not exist.\n");
    	}
    }

    For code 1
    I do code1, find the employee, calculate the pay for this particular employee..

    I do this for another employee, however the computed data doesnt go into the list..


    I tried code2 as well, but doesnt work well either.. the computed data isnt taken in, is there anything that i did wrongly in both of the codes? and which 1 should i be using ???

    Code2 is on the next reply

    Your help will be very much appreciated

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24
    Code:
    void calSinglePay(int empNo, struct PersonalData* user){
    	
    	struct PersonalData* currentEmp;
    	struct PersonalData* empData;
    	int exist = 0; 
    	int rate = 0;
    
    	struct CompData calculatedData;
    	calculatedData.otPay = 0;
    	calculatedData.weeklyPay = 0;
    
    	for (currentEmp=&*user; currentEmp!=NULL; currentEmp=currentEmp->nextElement){
    		if (currentEmp->code == empNo){
    			exist = 1;
    			empData = currentEmp;
    			break;
    		}
    		else
    			exist = 0;
    	}
    	if (exist = 1){
    		if (empData->hours > 40){
    			calculatedData.otHours = empData->hours - 40;
    
    			switch(empData->jobCat){
    				case 'a': case 'A':
    					rate = 10;
    					break;
    				case 'b': case 'B':
    					rate = 15;
    					break;
    				case 'c': case 'C':
    					rate = 20;
    					break;
    			}
    			if (calculatedData.otHours <= 5){
    				calculatedData.otPay += (rate * 1.5);
    			}
    			else if ((calculatedData.otHours > 5) && (calculatedData.otHours <= 10)){
    				calculatedData.otPay += (rate * 1.5);
    				calculatedData.otPay += (rate * 1.65);
    			}
    			else if ((calculatedData.otHours > 10) && (calculatedData.otHours <= 15)){
    				calculatedData.otPay += (rate * 1.5);
    				calculatedData.otPay += (rate * 1.65);
    				calculatedData.otPay += (rate * 1.75);
    			}
    			
    			else if (calculatedData.otHours > 15){
    				calculatedData.otPay += (rate * 1.5);
    				calculatedData.otPay += (rate * 1.65);
    				calculatedData.otPay += (rate * 1.75);
    				calculatedData.otPay += (rate * 1.6);
    			}
    
    			calculatedData.weeklyPay = empData->basicPay + calculatedData.otPay;
    		}
    
    		else
    		{
    			calculatedData.otHours = 0;
    			calculatedData.otPay = 0.00;
    			calculatedData.weeklyPay = empData->basicPay;
    
    		}
    		//Only can calculate 1 data, 2nd calculation corrupted	
    		&*user->dataElement = &calculatedData; 
    		printf("Pay successfully computed\n");
    
    	}
    
    	else{
    		printf("Employee does not exist.\n");
    	}
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    For code1:
    > &*user->dataElement = calculatedData;
    Here I believe you need:
    Code:
    		*(empData->dataElement) = calculatedData;
    Or you could also use:
    Code:
    		*(currentEmp->dataElement) = calculatedData;
    Code:
    >for (currentEmp=&*user; currentEmp!=NULL; currentEmp=currentEmp->nextElement){
    This is equivalent to:
    Code:
    for (currentEmp=user; currentEmp!=NULL; currentEmp=currentEmp->nextElement){

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24
    Hey thanks swoopy

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Alander View Post
    Hey thanks swoopy
    Glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 06:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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