I'm writing a simple program using if statements. I can't get the 2nd condition to give an accurate answer. I'm dividing two integer variables in an attempt to use the ceil() function on the answer to round up. I keep getting 1 when dividing 240/200. The first condition works but i honestly do not know why. The numbers i have been inputting to test are 150.99 for small package, 50.99 for large package and 240 for number of invitations. Thank you in advance.
#include <stdio.h>
#include <math.h>
#define TRUE 1
int main () {
int SMALL=50; // number of invitations in a small package
int LARGE=200.0000; // number of invitations in a large
int amount=0.0000; // total number of invitations
float costSmall=0.0; // cost of a small package
float costLarge=0.0; // cost of large package
printf("What is the cost of a small package (in dollars)?\n");
scanf("%f", &costSmall);
printf("What is the cost of a large package (in dollars)?\n");
scanf("%f", &costLarge);
printf("How many invitations are you sending?\n");
scanf("%d", &amount);
double a=0.00; //number of large packages
double b=0.00; //number of small packages
if (costSmall < costLarge) {
a = amount/LARGE;
b =(amount - a*LARGE);
ceil(a)
}
if (costSmall>costLarge) {
a=(amount/LARGE);
b=0;
}
printf("\nYou should buy %.0f small package(s).\n", b);
printf("You should buy %.0f large package(s).\n", a);
printf("Your total cost for invitations will be $%.2f", a*costLarge+b*costSmall);
printf("\n\n");
system("PAUSE");
return 0;
}
(double)amount / LARGE or the conventional C++ way: static_cast<double>(amount) / LARGE
If you explicitly cast either operand to a double it will implicitly cast the other and invoke the double / double operation that you want.
What do you think ceil(a) does? (Think carefully...) Also can you explain the decimal numbers on line 9 and 10? What is the point of assigning a compile-time constant double to an int variable?