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;
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);
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.
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);
#include <iostream>
int main() {
constint DOLLAR = 100; //pennies per dollar
constint 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;
}