Like Tree1Likes

Please help me with my C programming

This is a discussion on Please help me with my C programming within the C Programming forums, part of the General Programming Boards category; Hello, My name is Brandon. I'm new to programming, and I just started a course at college where I must ...

  1. #1
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    Exclamation Please help me with my C programming

    Hello,
    My name is Brandon. I'm new to programming, and I just started a course at college where I must write a C program. The only other program I've written was the helloworld program. I am having trouble completing my first assignment. I have tried for hours to get it to work properly, but im getting errors, and strange outputs. Basically, I am to write an app that helps estimate how much it would cost to paint a room. here is the description the teacher gave me:

    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.

    here is the code I've already written:
    Code:
    main() { /*Written by Brandon Benyacar*/
    
        int area, labour, length, width, height=8, ceiling, setup=100, paint;
        float total, room=;
    
        {printf("Painting Cost Estimator \n Enter length of room in feet: \n"); /*prompt user to input values*/
            scanf("%d", &length);
        printf("Enter width of room in feet: \n ");
            scanf("%d", &width);
        printf("Please select grade of paint to use for walls: \n 1 - Basic \n 2 - Regular \n 3 - Premium \n");
            scanf("%d", &paint);
        if(paint<=0 || paint>3) printf("Please select a grade of paint that actually exists!!! \n");
        printf("Enter if ceiling is to be painted: \n 1 Yes \n 2 No \n");
        scanf("%d", &ceiling);}
        
        {if(ceiling==1 &ceiling=(length*width)); /* Define whether ceiling is to be included in cost*/
        if(ceiling==2 &ceiling=0);}
    
    {if(paint==1 &paint=20);    /*assign cost to paint*/
    if(paint==2 &paint=30);
    if(paint==3 &paint=40);}
    
    {area=(2*length*height)+(2*width*height)+ceiling;
    printf("%d sq.feet \n", area);}
    room=((area400)*paint);
    printf ("Total %d", room);
     }
    I realise I am nowhere near complete, but I wanted to make sure I got rid of any errors now before I continue.
    Thanks in advanced for your help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    && means "that thing there and this thing here..."
    & means bitwise and (the bits in this and that that are both set)
    = means assign a value to a thing
    == means compare a value to another value
    ; after an if statement typically means you made a mistake.

    Now look at what you have:
    Code:
    if(paint==1 &paint=20);
    You should turn on your compiler's warnings.
    Code:
    room=((area400)*paint);
    What is that supposed to be? As it is, it think you have a variable named area400.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I believe that's a typo; I think the OP wants area divided by 400, judging from the question.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    ok, I have made a typo for area400, it was supposed to be value of area divided by 400. Paint I can now see is wrong, but im still not sure how to fix it. as is, the problems im running into are ceiling, gives me the same sq.foot whether I select to paint it or not, and the error message which is supposed to loop doesnt, and im not sure how to make it.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have a ton of problems with your code. Fix up all the stuff I mentioned, and then post your code. Also, your indentation is terrible, and you keep using random pairs of braces where you don't need them - which isn't technically wrong, it's just not necessary.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    I know my code is wrong, and I would fix up everything, but I dont know how. I learn best from example. thats why I'm asking for help :P
    please forgive my poor indentation and random brackets, I'm still just learning. I will try to fix as much as I can by myself in the meantime

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've already given examples of what is wrong. What do you mean to be happening here:
    Code:
    if(paint==1 &paint=20);
    if paint is one then make paint 20?
    Code:
    if( paint == 1 )
        paint = 20;
    You may also want to look into the else keyword:
    Code:
    if( paint == 1 )
        paint = 20;
    else
    if( paint == 2 )
        paint = 30;
    else
    if( paint == 3 )
        paint = 40;
    else
        ... you entered the wrong thing
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    thanks, that is better. now how can I get the error to loop back to ask the user the same question again? I know I need while, but am unsure how to implement it

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Bx2 View Post
    thanks, that is better. now how can I get the error to loop back to ask the user the same question again? I know I need while, but am unsure how to implement it
    You can use a while loop, where the condition is the user input. i.e., loop while user input is not equal to 'q' or whatever the user is supposed to enter to quite the calculations.

  10. #10
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    okay, so I've been messing around with the code, and I still cant get the hang of the do while loop. can anybody explain to me exactly what I'm doing wrong? also the total always equals $0. here is my current code:
    Code:
    main() { /*Written by Brandon Benyacar*/
    
    	int area, labour, length, width, height=8, ceiling, setup=100, paint;
    	float total, room;
    
    {printf("Painting Cost Estimator \n Enter length of room in feet: \n"); /*prompt user to input values*/
    scanf("%d", &length);
    	printf("Enter width of room in feet: \n ");
    	scanf("%d", &width);}
    	
    {{		printf("Please select grade of paint to use for walls: \n 1 - Basic \n 2 - Regular \n 3 - Premium \n"); /*assign cost to paint*/
    		scanf ( "%d", &paint );
    		
    			if( paint == 1 ) 
    				paint = 20;	
    		else
    			if( paint == 2 )
    				paint = 30;
    		else
    			if( paint == 3 )
    				paint = 40;
    			
    		else 
    		printf ("Please select a grade of paint that actually exists!!! \n");}
    			while ( paint <= 0 || paint > 3 );	}
    				
    				
    		
    	   
     
    { printf("Enter if ceiling is to be painted: \n 1 Yes \n 2 No \n");  /* Define whether ceiling is to be included in cost*/
    	scanf("%d", &ceiling);
    	while ( ceiling = 0 || ceiling > 2 );
    		if( ceiling == 1 )
    			ceiling = (length*width);
    	else
    		if( ceiling == 2 ) 
    			ceiling = 0;
    	else
    		printf ("You made an incorrect selection. Please choose whether the ceiling is to be painted: \n");}
    		
    {area=(2*length*height)+(2*width*height)+ceiling;
    printf("%d sq.feet \n", area);}
    
    {labour=(area/200)*2;
    room=((area)*paint)*2;
    total=room+setup+labour;
    printf ("Total: $%d \n", total);
    }
    }
    keep in mind ive been messing around with the code so not every little detail will be exactly as intended.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    $ gcc -Wall bar.c
    bar.c:3: warning: return type defaults to ‘int’
    bar.c: In function ‘main’:
    bar.c:35: warning: suggest parentheses around assignment used as truth value
    bar.c:50: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    bar.c:52: warning: control reaches end of non-void function

    Line 35
    while ( ceiling = 0 || ceiling > 2 );

    Line 50
    printf ("Total: $%d \n", total);
    quzah likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    good evening folks. I've learned a lot from this page. However I was wondering if someone could lend me a hand as well. I've been assigned the same question in my C Programming class and unfortunately the professor is unwilling to help (well says hes unable to).

    in anycase I actually have the same thing in terms of how Brandon uses paint.

    Code:
             if( paint == 1 )
                     paint = 40;
        else
            if( paint == 2 )
                     paint = 30;
        else
            if( paint == 3 )
                     paint = 20;
    now my options are "1" is for Premium paint charged at 40 dollars
    option "2" for Regular at 30 dollars
    option "3" for Basic at 20 dollars

    at the end of my program. I want to calculate the costs and totals.

    so the results should look like this

    RESULTS:
    Total Area: 1382 sq ft
    Premium paint cost: $ 80.00
    Regular paint cost: $ 76.80
    Basic paint cost: $ 47.00
    Labour costs: $376.40
    TOTAL COST: $580.20

    what i have so far is:

    Code:
    {              area=(length*height)+ceiling;
                    printf("The total area is, %d sq.feet\n", area);
                    paint=
                    labour=(area/200)*2+setup;
                    printf("Labour cost: %d\n", labour);
                    room=(area*paint)*2;
                    printf("room total: %d\n", room);
                    total=room+labour;
                    printf ("Total: $%d\n", total);}

    now my issue is that i am unsure on how to incorporate paint into the formula based on the choices of paint the client requested.

    when i run my program my end result is

    The total area is, 122 sq.feet
    Labour cost: 100
    Room total: 0
    Total: $0

    now i think labour cost is wrong...because it says

    "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"

    so thats why in my formula i wrote
    Labour=(area/200)*2+setup

    divided by 200 for the per sq feet x 2 for the 2coats. 100 for the setup. I dont know where to add that "plus" 40. Im at a total loss lol.

    And just as important, I dont know how to effectively make my questions loop until the user ends their selections for the totals :S.


    apologies for the long post. Id greatly appreciate any help. I am using Matrix. And nled for writing up my program.





  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @shadowboss... don't hijack threads... create your own.

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    Quote Originally Posted by CommonTater View Post
    @shadowboss... don't hijack threads... create your own.
    sorry i figured since its literally the same assignment, i should just post here figured someone would say to me "theres already a topic about this use search function" or something if i posted a new topic about the same thing.

    Should I still create my own or leave it since i already posted ? .

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    Quote Originally Posted by ShadowBoss View Post
    sorry i figured since its literally the same assignment, i should just post here figured someone would say to me "theres already a topic about this use search function" or something if i posted a new topic about the same thing.

    Should I still create my own or leave it since i already posted ? .

    nvm I figured out how to effectively use the Do-while loop and changed the || to && i had for ceiling and paint. and some other stuff....thanks in advance.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  2. Replies: 1
    Last Post: 08-19-2007, 04:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 11:32 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21