Thread: Need help with rounding in simple program

  1. #1
    Registered User
    Join Date
    Sep 2011

    Need help with rounding in simple program

    I've copied my code below, basically I'm making a program where the user gives the dimensions of the room to be painted, and the number of windows, doors, and skylights each have a standard area windows -12, doors -20, and skylights -24sqft. These are added together, and subtracted from the surface area of the room (Roof, and four walls). Then I must calculate how many gallons of paint is needed (220sqft / gal). After dividing the paint area by 220, I end up with the real value for example a 10x20x10 room with 3 windows, 2 doors, and 1 skylight, you need roughly 3.18 gallons. Now what i need is for the program to round the remainder up to 1, so that it would say 4 gallons, because you'll need to buy 4 to paint the room, because you can't buy .18 gallons these days lol. Now my class is still in the early steps of C, even though i know how to use if's and else's, I'm not supposed to know those yet. The way I have it now, it work, but if the area to be painted with windows, and etc. is subtracted, the program will output 1 less gallon then it actually needs since the mod of 220 & 220 is 0. I tried earlier using doubles, and using modifiers in front of the %lf, but I couldn't get it to round up always. If you could help me find a roughly break proof method to solving this dilemma, I would truly appreciate it.

    //Insert Header For code here:
    #include <stdio.h>
    int main() {
       printf("\n##### - ##### - ##### #### -\n"); //header
        int length, width, height, windows, doors, skylights, paintarea, gallons;
        // User Prompting section
        printf("\nEnter room length: ");
        printf("Enter room width: ");
        printf("Enter room height: ");
        printf("Enter number of windows: ");
        printf("Enter number of doors: ");
        printf("Enter number of skylights: ");
         // converting user values into area for subtraction from dimensions of room.
        windows = windows * 12;
        doors = doors * 20;
        skylights = skylights * 24;
        // Adding each set of walls, and roof to the total area to be painted in sq ft.
        paintarea = (width * height) * 2;
        paintarea = paintarea + ((length * height) * 2);
        paintarea = paintarea + (length * width);
        // Removing areas of windows, doors, and skylights.
        paintarea = paintarea - (windows + doors + skylights);
        // Dividing the totoal area by 220 since each gallon of paint cover 220 sqft, and will use printf formating to round the answer up to nearest gallon.
        gallons = paintarea / 220;
        gallons = gallons + ((paintarea%220) - ((paintarea%220)-1));
        printf("\nThe number of gallons needed would be %d. \n",gallons);
        return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Assign it to an integer. Subtract the integer from the original number. If what is left isn't zero, you need another gallon. Basically just always add one more gallon. You can always return the unused paint for a refund at your local hardware store in most cases anyway.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. rounding program...s.o.s.
    By pancho in forum C Programming
    Replies: 3
    Last Post: 02-11-2002, 10:22 PM

Tags for this Thread