1
#include <stdio.h>

int main(void)
{
    float price, vat, vatless, vatprice;
    int vatpercentage;

    printf("Enter the price of the item:\n ");
    scanf_s("%f", &price);

    printf("what is the current vat:  \n");
    scanf_s("%d", &vatpercentage);

    vatpercentage = (float) (vatpercentage / 100);
    vat = (float) (vatpercentage * vatpercentage);
    
    vatless = price / vat;

    vatprice = price - vatless;

    printf("Total = %.2f\n", price);
    printf("Your vatless price is %.2f\n", vatless);
    printf("your vatprice is %.2f\n", vatprice);

    return 0;
}

Hello trying to make a vat calculator but I can't really figure out why I'm only getting inf and - inf when I try this code.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
dundern
  • 37
  • 3
  • 2
    Do basic debugging. Run your program in a debugger and/or even add basic debugging print statements to examine the variable values. Specifically, have you had a look at `vatpercentage` value? Is it what you expect it to be? – kaylum Sep 13 '22 at 21:41
  • Also, avoid `float` for this task. Use `double`. – chux - Reinstate Monica Sep 13 '22 at 22:14
  • I don't understand why you multiply vatpercentage by itself in this line: `vat = (float) (vatpercentage * vatpercentage);` – Jerry Jeremiah Sep 13 '22 at 22:48
  • You should not use floating points for economic calculations as it will yield rounding errors. – Pablo Sep 13 '22 at 23:07
  • @Pablo If not done thoughtfully, integer calculations can yield truncation errors. Jus' saying... – Fe2O3 Sep 14 '22 at 04:05

2 Answers2

0

vatpercentage is an int, so the division vatpercentage / 100 is done as integer division. Define it as a float and you should be OK.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Simplicity is beneficial... I am lost in the tangle, computing values for 5 different variables with similar names.

Try this:

int main() {
    double price;
    printf( "Enter the price: " );
    scanf( "%lf", &price );

    int vat;
    printf( "Enter the VAT %%: ");
    scanf( "%d", &vat );

    double base = price * 100 / ( 100 + vat );
    printf( "Total%9.2lf\n", price );
    printf( "VAT  %9.2lf (%d%%)\n", price - base, vat );
    printf( "Base %9.2lf\n", base );

    return 0;
}

Output

Enter the price: 68.95
Enter the VAT %: 13
Total    68.95
VAT       7.93 (13%)
Base     61.02

Error checking and validating the input have been omitted for brevity.

"Floating point" will (usually) be a close approximation of what we humans regard as "integer values". There may be values that are shown as "5.00", but, internally, the value is "4.9998". The user is warned and, with the inclusion of "math.h", computed floating point values should be rounded appropriately.

I don't have experience with "VAT", but I think these 3 variables are computing what you are seeking. Please let me know if this is wrong and I will delete this answer as quickly as possible.

Fe2O3
  • 6,077
  • 2
  • 4
  • 20
  • 2
    With _money_, all 3 moneys should be rounded to the near base amount (e.g. 0.01). For select prices and VAT, base money may not be possible that sums as desired. – chux - Reinstate Monica Sep 14 '22 at 01:18
  • @chux-ReinstateMonica I've added a paragraph to the answer with a warning about floating point values. Thank you for checking up on me... "More sufficient unto the day" `:-)` – Fe2O3 Sep 14 '22 at 01:29