c - Float is printf wrong value -


i have float output issue printing "1.#r", have narrowed down recurring values, question how round value 2 decimal places, can print value (but not using "%.2f"). here 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 wage: \n");    scanf("%f",&wages);    if(wages > 9000){     tax_deduction(&wages);    }    hourly_rate(&hours);    printf("wages after tax , ni: %.2f\n", wages);    printf("wages per month: %.2f\n", wages / 12);    printf("hourly rate: %.2f\n", hours);    return 0; } 

there issue way it's displaying after runs hourly_rate function, when code @ bottom printf("hourly rate: %.2f\n", (wages/52)/37/5); works thats not how want code it.

can think of why isn't working correctly?

your problem not printf. problem code's arithmetic incorrect due use on uninitialized values.

float wages; // wages not initialized float hours = wages; // hours assigned uninitialized value printf("please enter wage: \n"); scanf("%f",&wages); if(wages > 9000){     tax_deduction(&wages); } hourly_rate(&hours); // hours still not initialized 

you failing initialised hours.

my guess mean initialize hours after reading in wages. so, move assignment hours point in code wages defined.

float wages; printf("please enter wage: \n"); scanf("%f",&wages); if(wages > 9000){     tax_deduction(&wages); } float hours = wages; hourly_rate(&hours); 

the strange output getting result of printing floating point nan %.2f format. suggest move if(wages > 9000) test inside tax_deduction. have presently, part of tax deduction code has leaked calling code.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -