tags:

views:

93

answers:

2

Write a program that contains the loop

while (scanf("%1f", &salary) == 1) {.......}

within the body of the loop compute a 17% federal withholding tax and a 3% state withholding tax and print these values along with the corresponding salary. Accumulate the sums of all salaries and taxes printed. Print these sums after the program exits the while loop.

My current code:

float salary, federal_tax, state_tax, salary_after_tax, salary_sum, tax_sum, salary_after_tax_sum;
printf("Enter Salary: $");
while (scanf("%lf", &salary) == 1)
{
salary_sum = salary;
federal_tax = salary*(.17);
state_tax = salary*(.03);
tax_sum = federal_tax + state_tax;
salary_after_tax = salary - federal_tax - state_tax;
salary_after_tax_sum = salary_after_tax;
printf("Salary before tax = %lf", salary);
printf("Federal tax = %lf", federal_tax);
printf("State tax = %lf", state_tax);
printf("Salary after tax = %lf\n", salary_after_tax);
break;
}
printf("total salary before tax = %lf", salary_sum);
printf("total tax = %lf", tax_sum);
printf("total salary after tax = %lf\n", salary_after_tax_sum);
system ("pause");
return 0;

}

For some reason it doesn't work!!! any help would be appreciated!!! thx

+1  A: 

You should assign your ..._sum variable to 0 outside the loop, and increment them with += in the loop; what you're doing now instead is reassigning them every time through the loop, and that isn't summing!-)

Alex Martelli
doesn't work, I mean doesn't give me any valid outputs!!! sry.Thanks Alex, I added salary_sum = 0;num = 0;While .....salary_sum = salary;salary_sum+=salary;num++;but it still gives me zero for everything. sry I am just starting!
Beho86
+2  A: 

Your scanf specifier is %lf (your problem statement says %1f instead, is that intentional?) and yet you're storing into a float. My scanf(3) says %lf specifies a double.

Incidentally, float has much less precision than many programmers may expect, just about seven digits, so using double instead is probably a good idea for salaries.

sarnold