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?
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?
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.
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");} }
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.
Second, get a compiler that will diagnose many more problems before you even get to running it.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"); } }
These are things which need to be fixed.
All the red ones mean that assuming it doesn't crash, it WILL be processing garbage data.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
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.
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.
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’
Let's look at the first one in the list, line 49: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
You declared "premium" as a float:
but now you're passing it to printf(), as a %d - an int:Code:float total, room, basic = 0.00, regular = 0.00, premium = 0.00;
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).Code:printf("Premium paint cost: $%d \n", premium);
Last edited by Adak; 10-16-2011 at 12:16 PM.