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.