Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 10-15-2011
Bx2
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?
• 10-15-2011
Salem
• 10-16-2011
Bx2
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");} }```
• 10-16-2011
Salem
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.
• 10-16-2011
Bx2
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.
• 10-16-2011
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.

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).
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12