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; hey shadowboss, Ive fixed up a lot of my code, but I too am having the issue where all my ...

  1. #16
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    hey shadowboss,
    Ive fixed up a lot of my code, but I too am having the issue where all my totals at the end are $0. how did you fix it?

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Post your latest code.
    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.

  3. #18
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    here is my entire program so far:
    Code:
    main() { /*Written by Brandon Benyacar*/
    
        int area, labour, length, width, height=8, ceiling, setup=100, paint;
        float total, room, basic=0.00, regular=0.00, premium=0.00;
    
    { 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("Enter Quality: \n 1 - Basic \n 2 - Regular \n 3 - Premium \n"); /*assign cost to paint*/
        
            scanf ( "%d", &paint);
            while ( paint <= 0 || paint > 3 )
    {        
                if( paint == 1 ) 
                    basic = 20.00;    
            else
                if( paint == 2 )
                    regular = 30.00;
            else
                if( paint == 3 )
                    premium = 40.00;
            
            else 
        
            printf ("Error! Quality must be between 1 and 3 \n");
            printf("Enter Quality: \n 1 - Basic \n 2 - Regular \n 3 - Premium \n");
            scanf ( "%d", &paint);
            }
                
    
    
     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 ("Error! Enter 1 to paint ceiling, 2 for no: \n");
            printf("Enter if ceiling is to be painted: \n 1 Yes \n 2 No \n");
            scanf("%d", &ceiling);
            }
            
    area=(2*length*height)+(2*width*height)+ceiling;
    total=room+labour;
    {        labour=(40*(area/200)*2)+setup;
                    printf("Labour cost: %d\n", labour);
                    room=(area*paint)*2;
            printf("Total Area: %d sq.feet \n", area);
            printf("Premium paint cost: $%d \n", premium);
            printf("Regular paint cost: $%d \n", regular);
            printf("Basic paint cost: $%d \n", basic);
                    printf ("Total: $%d\n");}
    }

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Step 1 is learn about indentation. Good indentation makes the code easier to write, and easier for others to read when you run into problems.
    Code:
    #include <stdio.h>
    main()
    {                               /*Written by Brandon Benyacar */
      int area, labour, length, width, height = 8, ceiling, setup = 100, paint;
      float total, room, basic = 0.00, regular = 0.00, premium = 0.00;
    
      {
        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("Enter Quality: \n 1 - Basic \n 2 - Regular \n 3 - Premium \n"); /*assign cost to paint */
      scanf("%d", &paint);
      while (paint <= 0 || paint > 3) {
        if (paint == 1)
          basic = 20.00;
        else if (paint == 2)
          regular = 30.00;
        else if (paint == 3)
          premium = 40.00;
        else
          printf("Error! Quality must be between 1 and 3 \n");
        printf("Enter Quality: \n 1 - Basic \n 2 - Regular \n 3 - Premium \n");
        scanf("%d", &paint);
      }
    
      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("Error! Enter 1 to paint ceiling, 2 for no: \n");
        printf("Enter if ceiling is to be painted: \n 1 Yes \n 2 No \n");
        scanf("%d", &ceiling);
      }
    
      area = (2 * length * height) + (2 * width * height) + ceiling;
      total = room + labour;
      {
        labour = (40 * (area / 200) * 2) + setup;
        printf("Labour cost: %d\n", labour);
        room = (area * paint) * 2;
        printf("Total Area: %d sq.feet \n", area);
        printf("Premium paint cost: $%d \n", premium);
        printf("Regular paint cost: $%d \n", regular);
        printf("Basic paint cost: $%d \n", basic);
        printf("Total: $%d\n");
      }
    }
    Second, get a compiler that will diagnose many more problems before you even get to running it.
    These are things which need to be fixed.
    Code:
    $ gcc -Wall bar.c
    bar.c:3: warning: return type defaults to ‘int’
    bar.c: In function ‘main’:
    bar.c:49: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    bar.c:50: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    bar.c:51: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    bar.c:52: warning: too few arguments for format
    bar.c:54: warning: control reaches end of non-void function
    bar.c:43: warning: ‘labour’ is used uninitialized in this function
    bar.c:43: warning: ‘room’ is used uninitialized in this function
    All the red ones mean that assuming it doesn't crash, it WILL be processing garbage data.
    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.

  5. #20
    Bx2
    Bx2 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    ok, but please realize this is an assignment worth marks, and I will get many more marks if the program works entirely, and will lose only some marks for the indentation/inefficient code. So please help me with my total always equating to 0. infact, all numbers the program reports are incorrect, and I cant figure out why.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, but please realize, there is an algorithm to how to create and debug a program. It starts with good indentation.

    Why?

    Because (and you won't learn this in your class), you eye (brain really), will automatically start recognizing patterns - in this case, syntax patterns in C. As you get more time doing it, you "eye" becomes more finely attuned to use these patterns which you have subconsciously learned, to great advantage. They will save you a LOT of time and effort - but only if your code has good indentation, so it fits into those patterns.

    Truth is, humans do visible light processing that leaves computers in the dust.

    On to your program!

    Salem has highlighted the errors and warnings from the compiler, about your program. That is the reason your computations are shot to hell.

    You need to give each function (printf(), scanf(), whatever), the correct format: int, double, whatever.

    You haven't done that:
    $ gcc -Wall bar.c
    bar.c:3: warning: return type defaults to ‘int’
    Code:
    bar.c: In function ‘main’:
    bar.c:49: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    bar.c:50: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    bar.c:51: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    bar.c:52: warning: too few arguments for format
    bar.c:54: warning: control reaches end of non-void function
    bar.c:43: warning: ‘labour’ is used uninitialized in this function
    bar.c:43: warning: ‘room’ is used uninitialized in this function
    Let's look at the first one in the list, line 49:

    You declared "premium" as a float:
    Code:
    float total, room, basic = 0.00, regular = 0.00, premium = 0.00;
    but now you're passing it to printf(), as a %d - an int:
    Code:
    printf("Premium paint cost: $%d \n", premium);
    So, change %d to the right format for double: %lf (el + f, not 1f), and that fixes one problem. Continue on and fix the rest, OK. The line numbers point you right to the correct line (sometimes, they will be off, but not here).
    Last edited by Adak; 10-16-2011 at 12:16 PM.

Page 2 of 2 FirstFirst 12
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, 02:46 AM
  2. Replies: 1
    Last Post: 08-19-2007, 03: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, 10: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, 10: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