Logic Error

Hi! Good day everyone!
Well, I'm a newbie and a freshman in Computer Science.
We're having this lab exercise that our Miss gave us.
and I'm just wondering what's wrong with my code.
I'm too shy to ask her.
So I was hoping that y'all can help me instead.. hehe..
I'm not sure what I did wrong. But when I choose the Cube formula,
the Area and Volume are giving me zero results!
I've done it correctly. I just need someone to tell me what exactly is wrong with my programming. So annoyed. I can't see it! Please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#define PI 3.14
void main()
{
	char a,b,c,d,e,q,l=0;
	int r,h,le,w,p,base,s;
	double A,V;
	printf("Shapes\n");
	printf("a. Sphere\nb. Cuboid\nc. Cube\nd. Cylinder\ne. Square Pyramid\n");
	
	while (l!='q')
	{
		printf("\nThe shape to calculate: ");
		scanf("%c",&l);
		
	if (l=='a'){
		printf("Enter r: ");
		scanf("%d",&r);
		printf("Enter h: ");
		scanf("%d",&h);
		A=4/3*PI*r*r;
		printf("Area: %.2f\n",A);
		V=4/3*PI*r*r*r;
		printf("volume: %.2f\n",V);	
		scanf("%c",&l);}
	else if (l=='b'){
		printf("Enter length: ");
		scanf("%d",&le);
		printf("Enter width: ");
		scanf("%d",&w);
		printf("Enter height: ");
		scanf("%d",&h);
		A=(2*w*le)+(2*h*le)+(2*w*h);
		printf("Area: %.2f\n",A);
		V=le*w*h;
		printf("Volume: %.2f\n",V);
		scanf("%c",&l);}
	else if (l=='c'){
		printf("Enter side: ");
		scanf("%d",&p);
		A=(6*p*p);
		V=(p*p*p);
		printf("Area: %d\n",A);
		printf("Volume: %d\n",V);
		scanf("%c",&l);}
	else if (l=='d'){
		printf("Enter Radius: ");
		scanf("%d",&r);
		printf("Enter height: ");
		scanf("%d",&h);
		A=(2*PI*r*h)+(2*PI*r*r);
		printf("Area: %.2f\n",A);
		V=PI*r*r*h;
		printf("Volume: %.2f\n",V);
		scanf("%c",&l);}
	else if (l=='e'){
		printf("Enter base: ");
		scanf("%d",&base);
		printf("Enter side: ");
		scanf("%d",&p);
		printf("Enter height: ");
		scanf("%d",&h);
		A=(2*base*p)+(base*base);
		printf("Area: %.2f\n",A);
		V=1/3*base*h;
		printf("Volume: %.2f\n",V);
		scanf("%c",&l);}
	}
	printf("BYE\n");

}
Last edited on
Warnings
Make sure to compile with complaints turned up to the maximum.
a.c:3:6: warning: return type of 'main' is not 'int' [-Wmain]
a.c: In function 'main':
a.c:6:22: warning: unused variable 's' [-Wunused-variable]
a.c:5:17: warning: unused variable 'q' [-Wunused-variable]
a.c:5:15: warning: unused variable 'e' [-Wunused-variable]
a.c:5:13: warning: unused variable 'd' [-Wunused-variable]
a.c:5:11: warning: unused variable 'c' [-Wunused-variable]
a.c:5:9: warning: unused variable 'b' [-Wunused-variable]
a.c:5:7: warning: unused variable 'a' [-Wunused-variable]

The first one is complaining that line 3 does not read

   3 int main()

Don't forget to add return 0; to the end of main():

69
70
71
	printf("BYE\n");
	return 0;
}

The remaining complaints are about variables you have listed but do not use. Get rid of them:

5
6
	char l=0;
	int r,h,le,w,p,base;



Variable Names
Also, l is always a terrible variable name. And while mathematical formulae tend towards things like V=lwh, simply transposing that to V=le*w*h leaves it confusing anyway.

The code would be so much easier to read (and write) if you get just a little bit more descriptive. Like:

volume = length*width*height;

Just a tiny bit more typing. Compiles to the same stuff. Professor is more likely to give you a good grade. (Because it is prettier, yes. Welcome to real life.)


Area and Volume
Area is always the 2D space consumed by flat objects.
Volume is the 3D space consumed by 3D objects.

None of your objects are 2D. Therefore, none of your objects should report that they have an area.

Some of the options are confusing anyway. For example, when running the program, I chose to calculate the volume of a sphere:

The shape to calculate: a
Enter r: 1
Enter h: _

What am I supposed to understand by this question? A sphere is defined by it's radius. There is no standard mathematical convention for an h anywhere in a sphere's formula. I plugged in a 1 just to see what would happen.

The result was 3.14. (And why you are here complaining. I'll get back to this in a moment.)

I immediately examined the code that calculates the volume of a sphere:

1
2
3
4
5
6
7
8
9
10
	if (l=='a'){
		printf("Enter r: ");
		scanf("%d",&r);
		printf("Enter h: ");
		scanf("%d",&h);
		A=4/3*PI*r*r;
		printf("Area: %.2f\n",A);
		V=4/3*PI*r*r*r;
		printf("volume: %.2f\n",V);	
		scanf("%c",&l);}

You ask for h, but never use it. Why are you asking for it, then?


Integer vs floating point arithmetic
When you divide an integer by an integer, you get a quotient and a remainder.

    14 / 3 = 4 R 2 -- four with a remainder of two.

In programming, these are two different operations:

    quotient  = 14 / 3;  /* quotient now equals 4 */
    
remainder = 14 % 3;  /* remainder now equals 2 */

So, your code asking for things like 4/3 and 1/3 are not giving you the answer you think they are.

If you want a floating point value, at least one of the operands must be a floating point value. You can do this by putting a ".0" on it:

  23                volume=4.0/3*PI*radius*radius*radius;   65                volume=1.0/3*base*height;


Brace Style & Indentation
This is just a nit-pick, but your life will be so much easier if you stick braces { } on lines all by themselves. It makes things stand out, and other programmers can read your code much easier. You are already doing this on the main and while blocks, so why not on the if blocks?

Also, notice that you did not indent your if statements any further in than the while loop -- suggesting that the if statements are not dependent on the while.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	...
	while (choice!='q')
	{
		printf("\nThe shape to calculate: ");
		scanf("%c",&choice);
		
		if (choice=='a')
		{
			printf("Enter the radius: ");
			...
		}
		else if (choice=='b')
		{
			...
		}
	}
	printf("BYE\n");
	return 0;
}


Final nit-pick
Your code terminates when the user presses 'q'. But unless the user has read your source code, he had no idea that's how it is done. Make sure to tell the user:

   9        printf("a. Sphere\nb. Cuboid\nc. Cube\nd. Cylinder\ne. Square Pyramid\nq. quit");

Sure, it's not a shape, but users understand that.


Hope this helps.
Last edited on
Thank you soo much for correcting all my mistakes..
I am soo embarrassed now.. >_<
But the question I wanted to ask is about my Cube formula..
I'm getting 0 results for both Area and Volume..

and well, I've only depended myself on the material that our Ms gave to us to solve.. And the formulas were already there.. and thank you once again for being my second eye on this..

So far, I'm not yet a good programmer but isn't this a good way to start?
As for the variables, I thought that if it were shorter, the best it would be..
*Fine I'll just go along with the full descriptive variable~*
So if I put a formula, I don't need to initialize or declare it in the beginning?
double A,V;

About the warnings too, I didn't care about it till now that you've told me too. coz as long as my program worked and no fail, I'll just leave it as it is..

Oh and about the main() problem..
Does this mean I shouldn't write Void main() on questions like this?

It has been a GREAT help!! Thank you!!
You are sooooo AMAZING!!!!!

but I was hoping to know what I did wrong for the cube formulas.
Don't be embarrassed, you are just learning. You should see my mistakes.

Look at the format specifier when you try to print the volume:

printf("Volume: %d\n", V);

But V is a double, so you should have:

printf("Volume: %.2f\n", V);

Sorry I missed that the first time through.


(2) Yes, don't write void main().
Thaaaaaaaaaaaaaaank you sooo much!!
Wow!! It's a big relief to have that problem solved.. haha..
I guess this is what they say, We learn from our mistakes..

So in that case whenever we ask user to put input,
it should be int main() ?

Today we just learned how to make a new function for ourselves..
and somehow understood.. =) Just saying.. haha..
Thanks again!! XD
Topic archived. No new replies allowed.