How to get a decimal answer when dividing variables.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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;
}
Last edited on
(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.
Thanks for the quick reply. So I entered:
a = (double)amount/LARGE;
b = 0;
ceil(a);

I got the same answer. I apologize to make you spell it out but it seems im obviously missing something.
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?
Topic archived. No new replies allowed.