How to stop rounding

I have this program made and have to input a tax rate using whole integers, no floating values. However, when I calculate the tax value for the total purchased, I am unable to prevent the program from rounding the tax value to the nearest dollar.

What may I be doing wrong? Or, what code may help to prevent the rounding and allow for the value of the tax rate (12%) to calculate as it should.

// Lemonade program

#include <stdio.h>
main (void)
{
const int SMALL = 1000;
const int MEDIUM = 2000;
const int LARGE = 8000;
const int TAX = 12;

printf("Menu:\n");
printf("*******************\n");
printf("New-age lemonade sizes:\n");

int dollar = 1000;
int cents = 10;
int value_Sm = SMALL / dollar % cents;
int value_Med = MEDIUM / dollar % cents;
int value_Lrg = LARGE / dollar % cents;
printf("\tSmall: %2d\n", value_Sm);
printf("\tMedium: %1d\n", value_Med);
printf("\tLarge: %2d\n", value_Lrg);
printf("\n");

printf("Enter number of drinks purchased...\n");
int amount_Sm = 0;
printf("\t# Small: ");
scanf("%d", &amount_Sm);

printf("\t# Medium: ");
int amount_Med = 0;
scanf("%d", &amount_Med);

printf("\t# Large: ");
int amount_Lrg = 0;
scanf("%d", &amount_Lrg);

printf("\n");
printf("Your order is: \n");
printf("\tSmall: %d @ $%d each\n", amount_Sm, value_Sm);
printf("\tMedium: %d @ $%d each\n", amount_Med, value_Med);
printf("\tLarge: %d @ $%d each\n", amount_Lrg, value_Lrg);

printf("\n");
printf("Amount owing:\n");

int total_Sm = amount_Sm * value_Sm;
int total_Med = amount_Med * value_Med;
int total_Lrg = amount_Lrg * value_Lrg;
int subtotal = total_Sm + total_Med + total_Lrg;
// FIX TAX RATE NEED 0.12 KEEP GETTING 0
int tax_total = (subtotal * TAX) / 1000;
int total = subtotal + tax_total;
int cents_sub = 0;

printf("\tSubtotal: $%3d.%02d\n", subtotal, cents_sub);
printf("\tTax: \t $%3d.%02d setprecision(2)\n", tax_total);
printf("\tTotal: \t $%3d.%02d\n", total);
return 0;
}
Why don't you want to use floating point types? Unless you do, integer division will always result in an integer.
It's actually for a class assignment and we were instructed to use whole integer values instead of floating values.

There is no way to divide an integer to obtain a decimal to calculate further?

For example,
const int TAX = 120
int tax_rate = TAX / 1000;


No, there is no way unless you use 1000.0 instead of 1000, which would be cheating. Integers cannot hold decimal places.
I see, well if the instructions are stating that we are not to use floating numbers.

How would I present a tax rate of 12% (0.12) into this program?
Multiply the TAX in to all your calculations first, and then divide by 1000 at the last second.
I tried what you said and now it's apparently still rounding the value.
Such as, when I enter 1 quantity purchased of each size the tax should be $1.32 however it is shown as $1.00

Is there anyway to stop the rounding and showcase the cents value?
You could divide by 1000 to get the part before the decimal place, output the period, and then subtract that from the value divided by 10 to get the part after the decimal point.
still doesn't work.
Can you show me the code you have now?
This is what I have as of now:

// Lemonade program

#include <stdio.h>
main (void)
{
const int SMALL = 1000;
const int MEDIUM = 2000;
const int LARGE = 8000;
const int TAX = 12;

printf("Menu:\n");
printf("*******************\n");
printf("New-age lemonade sizes:\n");

int dollar = 1000;
int cents = 10;
int value_Sm = SMALL / dollar % cents;
int value_Med = MEDIUM / dollar % cents;
int value_Lrg = LARGE / dollar % cents;
printf("\tSmall: %2d\n", value_Sm);
printf("\tMedium: %1d\n", value_Med);
printf("\tLarge: %2d\n", value_Lrg);
printf("\n");

printf("Enter number of drinks purchased...\n");
int amount_Sm = 0;
printf("\t# Small: ");
scanf("%d", &amount_Sm);

printf("\t# Medium: ");
int amount_Med = 0;
scanf("%d", &amount_Med);

printf("\t# Large: ");
int amount_Lrg = 0;
scanf("%d", &amount_Lrg);

printf("\n");
printf("Your order is: \n");
printf("\tSmall: %d @ $%d each\n", amount_Sm, value_Sm);
printf("\tMedium: %d @ $%d each\n", amount_Med, value_Med);
printf("\tLarge: %d @ $%d each\n", amount_Lrg, value_Lrg);

printf("\n");
printf("Amount owing:\n");

int total_Sm = amount_Sm * value_Sm;
int total_Med = amount_Med * value_Med;
int total_Lrg = amount_Lrg * value_Lrg;
int subtotal = total_Sm + total_Med + total_Lrg;
int tax_Sm = total_Sm * TAX;
int tax_Med = total_Med * TAX;
int tax_Lrg = total_Lrg * TAX;
int tax_total = tax_Sm + tax_Med + tax_Lrg;
int tax_amount = tax_total / 100;
int total = subtotal + tax_amount;
int cents_sub = 0;

printf("\tSubtotal: $%3d.%02d\n", subtotal, cents_sub);
printf("\tTax: \t $%3d.%02d\n", tax_amount);
printf("\tTotal: \t $%3d.%02d\n", total);
return 0;
}
Heres how I would do it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main() {
	const int DOLLAR = 100; //pennies per dollar
	const int RATE = 12; //in perecent
	int price = 0; //price of the product you are purchasing
	int total_price = 0;
	int cents = 0; //the amount of change left over
	
	std::cout << "Please enter the price of the product you are purchasing($): ";
	std::cin >> price;
	
	total_price = price * ( DOLLAR + RATE ); //using formula ( 1 + rate ) multiplied by 100
	
	std::cout << "After tax you owe " << total_price << " cents." << std::endl;
	
	cents = total_price % DOLLAR;
	total_price /= DOLLAR; //removing the cents
	
	std::cout << "After tax you owe " << total_price << " dollars and " << cents << " cents." << std::endl;
	
	return 0;
}


http://ideone.com/SiPhOd
Topic archived. No new replies allowed.