I'm making a program that calculate wages, but I'm having problems in seeking the rates in the file, an example of one table.http://3.bp.blogspot.com/_xrZFmlhX0Ts/SYofqkmc1bI/AAAAAAAAO-Y/XfgESqYqZms/s400/tabela+2.JPG

the function receiving the salary and number of dependent, will seek in the file for the correct rate that this person as to pay.

I thought of doing if for the salary and then a switch (dependent) case, but in that case the code will be very long, because I'll have to do one if and switch case for salary range. And for that i changed the rates table(will be in txt file) like this to do what I've thought.

575,00 0,0 0,0 0,0 0,0 0,0 0,0
580,00 1,0 0,0 0,0 0,0 0,0 0,0
587,00 2,0 0,0 0,0 0,0 0,0 0,0
633,00 3,0 1,0 0,0 0,0 0,0 0,0
675,00 4,0 2,0 1,0 0,0 0,0 0,0
726,00 5,0 3,0 2,0 1,0 0,0 0,0
801,00 6,0 5,0 3,0 2,0 1,0 0,0
907,00 7,0 6,0 4,0 3,0 2,0 1,0
988,00 8,0 7,0 6,0 4,0 3,0 2,0
1.048,00 9,0 8,0 7,0 6,0 4,0 3,0


Thanks

575,00 0,0 0,0 0,0 0,0 0,0 0,0
580,00 1,0 0,0 0,0 0,0 0,0 0,0
587,00 2,0 0,0 0,0 0,0 0,0 0,0
633,00 3,0 1,0 0,0 0,0 0,0 0,0
675,00 4,0 2,0 1,0 0,0 0,0 0,0
726,00 5,0 3,0 2,0 1,0 0,0 0,0
801,00 6,0 5,0 3,0 2,0 1,0 0,0
907,00 7,0 6,0 4,0 3,0 2,0 1,0
988,00 8,0 7,0 6,0 4,0 3,0 2,0
1.048,00 9,0 8,0 7,0 6,0 4,0 3,0

OK, let's say the above is your array, called table. Income is the far left, and adjustment for number of dependents, is in the other columns.

so your logic could be, first, get the right row:

get toFind //income to find the rate for
while(toFind <= table[i++][0]); //note the semi-colon here

//you have gone one row too far, so decrement i
--i;
//and now you have the right income row.

Now, wasn't that easy? ;)

You can repeat the type of logic above, for dependents to get the adjustment right, but you can see that dependents = dependents +1 in the table, above, so.

dependents = 3;

get the right row, as shown above, then

income = table, and dependents adjustment = table[dependents+1]

It's easier, imo, if you load the data for the table, into a 2 dimension array[][], instead of working with the data in a file.

Edited 5 Years Ago by Adak: n/a

Based in what you said i did this code

while(lista->dados.salario <= salario_table)
	{
		fscanf(fp,"%f", &salario_table);
		if (lista->dados.salario <= salario_table)
			switch (lista->dados.dependentes)
			{
				case 0: fseek(fp,2*(sizeof(float)),SEEK_SET); break;
				case 1: fseek(fp,3*(sizeof(float)),SEEK_SET); break;
				case 2: fseek(fp,4*(sizeof(float)),SEEK_SET); break;
				case 3: fseek(fp,5*(sizeof(float)),SEEK_SET); break;
				case 4: fseek(fp,6*(sizeof(float)),SEEK_SET); break;
				default: fseek(fp,7*(sizeof(float)),SEEK_SET);break;
			}
		else
			fseek(fp, 9*(sizeof(float)),SEEK_SET);
	}
	fscanf(fp,"%f", &taxa);

But it didn't work, it only give-me 575,00, that's the first member of the table, the fseek's isn't working.
What I'm doing wrong?

BTW Thanks for replying.

Edit:

I found here the error

while(lista->dados.salario <= salario_table)

But when i did

while(lista->dados.salario >= salario_table)

the program enter in infinite loop.

What is wrong?

Thanks

Edited 5 Years Ago by acnodnem: n/a

This article has been dead for over six months. Start a new discussion instead.