Reputation: 399
I have another float output issue printing "1.#R", I have narrowed it down to recurring values, so my question is how do I round the value to the 2 decimal places, so I can print the value (but not using "%.2f"). Here is my code:
#include <stdio.h>
#include <stdlib.h>
void tax_deduction( float *total)
{
float taxpercent = 1.20;
float nationalInsurance = 1.175;
*total = *total - 9000;
*total = *total / taxpercent;
*total = *total / nationalInsurance;
*total = *total + 9000;
}
void hourly_rate(float *rate)
{
int weeks = 52;
float hours = 37.5;
*rate = *rate /(float)weeks;
*rate = *rate /hours;
}
int main()
{
float wages;
float hours = wages;
printf("Please enter your wage: \n");
scanf("%f",&wages);
if(wages > 9000){
tax_deduction(&wages);
}
hourly_rate(&hours);
printf("wages after tax and NI: %.2f\n", wages);
printf("wages per month: %.2f\n", wages / 12);
printf("hourly rate: %.2f\n", hours);
return 0;
}
There is an issue with the way it's displaying after it runs the hourly_rate function, as when I code it at the bottom as printf("hourly rate: %.2f\n", (wages/52)/37/5); it works but thats not really how I want to code it.
Can anybody think of why this isn't working correctly?
Upvotes: 0
Views: 2860
Reputation: 613451
Your problem is not the printf
. Your problem is that your code's arithmetic is incorrect due to the use on uninitialized values.
float wages; // wages not initialized
float hours = wages; // hours assigned that uninitialized value
printf("Please enter your wage: \n");
scanf("%f",&wages);
if(wages > 9000){
tax_deduction(&wages);
}
hourly_rate(&hours); // hours still not initialized
You are failing to initialised hours
.
My guess is that you mean to initialize hours
after reading in wages
. So, move the assignment to hours
to a point in the code where wages
is properly defined.
float wages;
printf("Please enter your wage: \n");
scanf("%f",&wages);
if(wages > 9000){
tax_deduction(&wages);
}
float hours = wages;
hourly_rate(&hours);
The strange output you are getting is the result of printing a floating point NaN with %.2f
format. I would also suggest that you move the if(wages > 9000)
test inside the tax_deduction
. As you have it presently, that part of the tax deduction code has leaked into the calling code.
Upvotes: 4
Reputation: 340426
To add to the other answers about the uninitialized variable:
Your printf()
is trying to print 1.#QNAN
(presumably because the value of the uninitialized variable happens to be a "quiet NAN"). But there's not enough space in the precision, so the 1.#QNAN
string is getting 'rounded up' to 1.#R
. See the following articles for details on this phenomenon:
Upvotes: 0
Reputation: 156
Yes, because you set hours = wages
before you read a value into wages. These are values bein set equal, not references.
If you insert hours = wages after the scanf line it will work.
Further, as a matter of coding style, in simple cases like these most programmers would prefer using return values from functions instead of sending pointers to values that are modified. This is by no means universal, but probably a good idea.
Upvotes: 1