Thread: Simple C program Help

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Simple C program Help

    hi all, I'm new to C programming, first year in school and have hit a little snag in one of my assignments.

    This is the program I have right now

    Code:
    #include <stdio.h>
    
    
    
    int main() {
    
    
    
    int length, width, quality, ceiling, area;  /* declares variables */
    
    
    
    /* initializes variables */
    
    float premium = 0.00;
    
    float regular = 0.00;
    
    float basic = 0.00;
    
    float labour = 100.00;
    
    double total = 0.00;
    
    
    
    printf("PAINTING COST ESTIMATOR \n");
    
    printf("----------------------------------- \n");
    
    
    
    /* do while loop which asks for length of room in feet and keeps going
    
    until a positive value greater than 0 is entered */
    
    do {
    
    
    
        printf("Enter length of room in feet: ");
    
        scanf("%d", &length);  /* stores value of length */
    
        if (length <= 0){
    
            printf("Error! Must be a positive value\n "); /* prints error message if value is less than or equal to 0 */
    
        }
    
    
    
    }while (length <= 0);
    
    
    
    
    
    /* same as above except for length */
    
    do {
    
        printf("Enter width of room in feet: ");
    
        scanf("%d", &width);
    
        if (width <= 0){
    
            printf("Error! Must be a positive value \n");
    
        }
    
    
    
    }while (width <= 0);
    
    
    
    /* do while loop to determine quality of paint, 1, 2 or 3 */
    
    do { 
    
        printf("Enter quality (1:premium, 2:regular, 3:basic): ");
    
        scanf("%d", &quality);
    
        if (quality <= 0 || quality > 3){
    
            printf("Error! Quality must be between 1 and 3 \n");
    
        }
    
    }while (quality <= 0 || quality > 3);  /* loop keeps going until value is equal to 1, 2 or 3 */
    
    
    
    /* asks user if s/he wishes to paint ceiling by entering 1 or 2 */
    
    do {
    
    
    
        printf("Enter if ceiling to be painted (1:yes / 2:no): ");
    
        scanf("%d",&ceiling);
    
        if (ceiling <= 0 || ceiling >= 3){
    
            printf("Error! Please enter 1 for Yes or 2 for No \n");
    
        }
    
    }while (ceiling <= 0 || ceiling >= 3);
    
    
    
    
    
    
    
    printf("RESULTS: \n");
    
    
    
    /* determines if user entered 1 or 2 for ceiling to be painted */
    
    if (ceiling == 1 && area <= 400){
    
        area = 2 * (length * 8) + 2 *(width * 8) + (length * width);/* determines square feet total */
    
        printf("Total Area: %d \n", area); /* stores value of sq ft */
    
    
    
    }else if (ceiling == 1 && area > 400){
    
        area = 2 * (length * 8) + 2 *(width * 8) + (length * width);/* determines square feet total */
    
        printf("Total Area: %d \n", area); /* stores value of sq ft */
    
          
    
    }else if (ceiling == 2){
    
             area = 2 * (length * 8) + 2 * (width * 8);/* determines sq ft total if ceiling is not included */
    
             printf("Total Area :%d \n", area);
    
        
    
    /* calculates total paint cost, determined by which quality of paint user entered */
    
    
    
    }if (quality == 1 && area < 400){ /* determiens if quality is premium and area is less than 400 */
    
        premium = 40.00 * 1;       /* determines premium cost if if statement above is true */
    
        printf("Premium paint cost: %.2f \n", premium);  /* prints these if statement above is true */
    
        printf("Regular paint cost: 0.00 \n");
    
        printf("Basic paint cost: 0.00 \n");
    
    
    
    }else if (quality == 1 && area >= 400){
    
        premium = 40.00 * 2;
    
        printf("Premium paint cost: %.2f \n", premium);
    
        printf("Regular paint cost: 0.00 \n");
    
        printf("Basic paint cost: 0.00 \n");
    
    
    
    }else if (quality == 2 && area < 400) {
    
        regular = 30.00 * 1;
    
        printf("Premium paint cost: 0.00 \n");
    
        printf("Regular paint cost: %.2f \n", regular);
    
        printf("Basic paint cost: 0.00 \n");
    
    
    
    }else if (quality == 2 && area >= 400){
    
        regular = 30.00 * 2;
    
        printf("Premium paint cost: 0.00 \n");
    
        printf("Regular paint cost: %.2f \n", regular);
    
        printf("Basic paint cost: 0.00 \n");
    
    
    
    } else if (quality == 3 && area < 400) {
    
        basic = basic + 20.00 * 1;
    
        printf("Premium paint cost: 0.00 \n");
    
        printf("Regular paint cost: 0.00 \n");
    
        printf("Basic paint cost: %.2f \n", basic);
    
    
    
    }else if (quality == 3 && area >= 400){
    
        basic = basic + 20.00 * 2;
    
        printf("Premium paint cost: 0.00 \n");
    
        printf("Regular paint cost: 0.00 \n");
    
        printf("Basic paint cost: %.2f \n", basic);
    
        }
    
    
    
    labour = labour + 40.00 * (area / 200); /* determines total labour costs and prints it */
    
    printf("Labour Cost: %.3f \n", labour);
    
          
    
    
    
    
    
    total = premium + regular + basic + labour;  /* determines total cost and prints it */
    
    printf("TOTAL COST: %.2f \n", total);
    
    
    
        return 0;
    
    
    
    
    
        }

    now the question for the assignment is


    You are to write a program to assist a painting contractor to prepare quotes for painting
    jobs. The quotes are NOT to include HST. The painter always applies 2 coats of paint to
    areas being painted. Painting costs depend on area to be painted and quality of paint to be
    used. 1 or more rooms may be painted. You can assume all walls to be painted have a
    height of 8 feet. Ceilings are painted using a basic grade of paint at $20 per 400 square
    feet. Wall paint also covers 400 square feet and 3 qualities of wall paint are available;
    premium at $40, regular at $30 and basic at $20. Labour costs include a setup cost of $100
    plus $40 per 200 square feet (includes 2 coats) to be painted. Paint is obtained in bulk so
    the quote is to only include paint used. An error message is to be displayed if any invalid
    input is entered and input is to be prompted for and re-input until valid.


    My output from the program is:
    PAINTING COST ESTIMATOR

    -----------------------------------

    Enter length of room in feet: 15
    Enter width of room in feet: 10
    Enter quality (1remium, 2:regular, 3:basic): 2
    Enter if ceiling to be painted (1:yes / 2:no): 1

    RESULTS:
    Total Area: 550
    Premium paint cost: 0.00
    Regular paint cost: 60.00
    Basic paint cost: 0.00
    Labour Cost: 180.000
    TOTAL COST: 240.00


    But I am supposed to get this :

    PAINTING COST ESTIMATOR
    Enter length of room in feet: 15
    Enter width of room in feet: 10
    Enter quality (1remium, 2:regular, 3:basic): 2
    Enter if ceiling to be painted (1-yes / 2-no): 1
    RESULTS:
    Total Area: 550 sq ft
    Premium paint cost: $ 0.00
    Regular paint cost: $ 60.00
    Basic paint cost: $ 15.00
    Labour costs: $210.00
    TOTAL COST: $285.00

    What am I doing wrong can someone please help!!

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I think the problem is with integer division. In C, if you divide an integer by another, you get an integer and the remainder is discarded. So 9 divided by 4 would be 2, not 2.25.

    Try making the denominator explicitly a float; something like this:

    Code:
    some_float = some_integer / 5; /* WRONG */
    some_float = some_integer / 5.0; /* BETTER */
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Quote Originally Posted by TheBigH View Post
    I think the problem is with integer division. In C, if you divide an integer by another, you get an integer and the remainder is discarded. So 9 divided by 4 would be 2, not 2.25.

    Try making the denominator explicitly a float; something like this:

    Code:
    some_float = some_integer / 5; /* WRONG */
    some_float = some_integer / 5.0; /* BETTER */
    Oh sweet yea that was it, changed the labour line from area / 200 to area / 200.00 and i got it.

    But now I'm missing the $15 from the basic costs, any ideas?

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Look at this bit of code

    Code:
    /* determines if user entered 1 or 2 for ceiling to be painted */
     
    if (ceiling == 1 && area <= 400){
     
        area = 2 * (length * 8) + 2 *(width * 8) + (length * width);/* determines square feet total */
     
        printf("Total Area: %d \n", area); /* stores value of sq ft */
     
     
     
    }else if (ceiling == 1 && area > 400){
     
        area = 2 * (length * 8) + 2 *(width * 8) + (length * width);/* determines square feet total */
     
        printf("Total Area: %d \n", area); /* stores value of sq ft */
     
           
     
    }else if (ceiling == 2){
     
             area = 2 * (length * 8) + 2 * (width * 8);/* determines sq ft total if ceiling is not included */
     
             printf("Total Area :%d \n", area);
    Your first if/else if is wrong. First you are asking it if the area is less than 400 but you haven't even calculated it yet. And both "area <400" and "area >= 400" cases do exactly the same thing. Combine both of these into one case that only cares whether ceiling==1.

    Now, you want to paint all the walls with a user-specified paint, but if you paint the ceiling it has to be with the basic paint. So what you need to do is, if the ceiling is to be painted calculate the area of the ceiling and work out how much that costs in basic paint.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    soo like

    Code:
    switch (ceiling == 1)
    
    case 1: area = 2 * (length * 8) + 2 *(width * 8) + (length * width);

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Switch statements work like this:

    Code:
    switch( choice ) {
       case 1:
       /* do something */
       break;
       case 2:
       /* do another thing */
       break;
       default:
       /* if the choice doesn't match any of the above */
    }
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Oh yea, sorry wasnt sure.

    Code:
    switch( ceiling ) {
    
       case 1:
     area = 2 * (length * 8) + 2 *(width * 8) + (length * width);
    
       break;
       case 2:
       area = 2 * (length * 8) + 2 * (width * 8)
    
       break;
       default:
       /* if the choice doesn't match any of the above */
    }
    ye?

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    So far so good. But you still need to calculate the area of just the ceiling and store that in a different variable, because that uses the basic paint no matter what you decide for the walls.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Also you should just set the basic cost at a certain variable and then just reference it to the others so you can always add the basic cost to your other selections.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    slowly getting there.. and I'd also have to declare the variable ceiling_area, so just picture it being already done

    Code:
    switch( ceiling ) {     
    case 1:  area = 2 * (length * 8) + 2 *(width * 8) + (length * width); 
    ceiling_area = length * width;    
    break;    
    case 2:    area = 2 * (length * 8) + 2 * (width * 8)     
    break;    
    default:    /* if the choice doesn't match any of the above */ }
    Also you should just set the basic cost at a certain variable and then just reference it to the others so you can always add the basic cost to your other selections.
    as in #DEFINE basic 20? for example

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Quote Originally Posted by NewbieCProgramm View Post
    slowly getting there.. and I'd also have to declare the variable ceiling_area, so just picture it being already done

    Code:
    switch( ceiling ) {     
    case 1:  area = 2 * (length * 8) + 2 *(width * 8) + (length * width); 
    ceiling_area = length * width;    
    break;    
    case 2:    area = 2 * (length * 8) + 2 * (width * 8)     
    break;    
    default:    /* if the choice doesn't match any of the above */ }

    as in #DEFINE basic 20? for example


    I would not recommend that as you might want to use it for another function. Rather in the main function set it to
    Code:
     double base = 20.00;
    This way basic will always be 20.
    Now all you have to do is set the equation to
    Code:
    else if (quality == 3 && area < 400) 
    {      
    basic = base + 20.00 * 1;     
    printf("Premium paint cost: 0.00 \n");      
    printf("Regular paint cost: 0.00 \n");      
    printf("Basic paint cost: %.2f \n", basic);   
    }
    
    else if (quality == 3 && area >= 400)
    {     
     basic = base + 20.00 * 2;      
    printf("Premium paint cost: 0.00 \n");      
    printf("Regular paint cost: 0.00 \n");      
    printf("Basic paint cost: %.2f \n", basic);    
      }
    Last edited by zach48191; 11-03-2011 at 10:47 PM.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    how do I get the basic cost to be 15.00? I can't figure out where to put it. It's only if the ceiling is being painted

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    bump, need help lol

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Bump

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    nvm problem solved..

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. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM