1

It is really basic stuff, im just stuck and cant procede.

Im trying following calculation, but it just wont work:

#include<stdio.h>

int main(){

    double amount;
    double interest_rate;

    double tax_d = (amount * interest_rate) / 100;
    float tax_f = (amount * interest_rate) / 100;


    printf("\n write amount: ");
    scanf("%lf" , &amount);
    printf("\n write interest rate: ");
    scanf("%lf", &interest_rate);
    printf("Tax (double) : %lf\n" , tax_d);
    printf("Tax (float) : %lf\n" , tax_f);


    return 0;

}

This doesnt work properly:

double tax_d = (amount * interest_rate) / 100;
float tax_f = (amount * interest_rate) / 100;

Any help will be appreciated.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
marinator
  • 31
  • 4
  • When you say _This doesnt work properly:_, this is rather unspecific. Please, describe instead: What is expected (and why). What did you get instead. – Scheff's Cat Nov 14 '18 at 18:03
  • 4
    why you are calculating before reading from input? – Afshin Nov 14 '18 at 18:04
  • Btw. your computation is done before you read the input values. You should re-order your code. As it is, it surely cannot work properly. – Scheff's Cat Nov 14 '18 at 18:05
  • In general, you can assume that your code in `main()` is executed from top to bottom. (That's why, C is counted as imperative language with defined control flow.) Nowadays, there are lots of discussions about re-ordering in generated code. However, every re-ordering the compiler may do for optimization may not violate your model from top to bottom execution. (roughly described) – Scheff's Cat Nov 14 '18 at 18:07
  • See https://stackoverflow.com/a/53238897/1848654. – melpomene Nov 14 '18 at 18:12

1 Answers1

1
double tax_d = (amount * interest_rate) / 100;
float tax_f = (amount * interest_rate) / 100;

These lines of code need to execute (be placed) after amount and interest_rate have been read and populated with user data

Specifically after these 2 lines:

scanf("%lf" , &amount);
scanf("%lf", &interest_rate);

It would also be good practice to initialize those values (i.e. double amount = 0 and double interest_rate = 0) upon their declaration..

*For future reference: Edit the question to be more specific as to what your inputs are and what you were expecting as output. This helps the community in driving to what the problem is and the solution.

melpomene
  • 84,125
  • 8
  • 85
  • 148
static_cast
  • 1,174
  • 1
  • 15
  • 21