0

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.

4
  • Homework questions are acceptable, but please tag them as such! Commented Aug 4, 2010 at 2:28
  • "it doesn't work" is a little sparse. Can you be more specific? Commented Aug 4, 2010 at 2:30
  • 2
    I think breaking out of the loop after the first iteration defeats the purpose of having a loop. Perhaps you should remove the break and actually collect multiple values from the user to sum. Commented Aug 4, 2010 at 2:35
  • @AlexMartelli the homework tag is deprecated.. Commented Mar 26, 2013 at 17:48

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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!-)

1 Comment

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!
0

You lied to the compiler and it got its revenge: the %lf format needs to go with a pointer-to-double, while you provide only a pointer-to-float. This means undefined behavior in C lingo (anything may happen).

Fix your float declarations by turning them into doubles.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.