Error in calculation of volume. Pls help!! Have to submit this as project in 2 days

Calculation of volume of cylinder is faulty. volume of others is okay. dont have the slightest idea bout the problem. here is the code.

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
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void area(float);
void area(float,float);
void area(float,float);
int ch;
float len,bre,hei,rad;
char ans;
do
{
cout<<"\nVOLUME MENU";
cout<<"\n\t1. Cube\n\t2. Cuboid\n\t3. Cylinder";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter length of cube:";
cin>>len;
volume(len);
break;
case 2:cout<<"Enter length x breadth x height of cuboid:";
cin>>len>>bre>>hei;
volume(len,bre,hei);
break;
case 3:cout<<"Enter radius x height of cylinder:";
cin>>len>>hei;
volume(rad,hei);
break;
default:cout<<"Wrong Input!";
}
cout<<"\nDo you want to continue? (Y/N):";
cin>>ans;
}while (ans=='y'||ans=='Y');
getch();
}
void volume(float l)
{
cout<<"Volume of cube = "<<(l*l*l)<<" cube units";
}
void volume(float l,float b,float h)
{
cout<<"Volume of cuboid = "<<(l*b*h)<<" cube units";
}
void volume(float r,float h)
{
cout<<"Volume of cylinder = "<<(3.14*r*r*h)<<" cube units";
}
Last edited on
None of the functions work, since the prototype declarations are missing?

please use code tags, [code]your code here[/code]
Calculation of volume of cylinder is faulty.

What exactly is wrong?

What values are you using to compute the volume?

rad = ?
hei = ?

What output does your program produce?

What output do you expect with the supplied values?

Perhaps you may want to look closely at this snippet (pay added attention to the second and third line)?
1
2
3
case 3:cout<<"Enter radius x height of cylinder:";
cin>>len>>hei;
volume(rad,hei);



Last edited on
You have function prototypes that overload an area function but you call and define later a volume function. Two of the prototypes are identical, but it seems one of the overloads should take three parameters?

Function prototypes can be pulled out of the main function and put before it. Main should also return int, not void (you may be using an older compiler?).

okay i'll update my code. still seems to be faulty

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
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void volume(float);
void volume(float,float,float);
void volume(float,float);
int ch;
float len,bre,hei,rad;
char ans;
do
{
cout<<"\nVOLUME MENU";
cout<<"\n\t1. Cube\n\t2. Cuboid\n\t3. Cylinder";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter length of cube:";
cin>>len;
volume(len);
break;
case 2:cout<<"Enter length x breadth x height of cuboid:";
cin>>len>>bre>>hei;
volume(len,bre,hei);
break;
case 3:cout<<"Enter radius x height of cylinder:";
cin>>len>>hei;
volume(rad,hei);
break;
default:cout<<"Wrong Input!";
}
cout<<"\nDo you want to continue? (Y/N):";
cin>>ans;
}while (ans=='y'||ans=='Y');
getch();
}
void volume(float l)
{
cout<<"Volume of cube = "<<(l*l*l)<<" cube units";
}
void volume(float l,float b,float h)
{
cout<<"Volume of cuboid = "<<(l*b*h)<<" cube units";
}
void volume(float r,float h)
{
cout<<"Volume of cylinder = "<<(3.14*r*r*h)<<" cube units";
}


jlb, i gave rad as 10 and height as 30. output:

Volume of cylinder = 4,5636e-17 cube units

actual value from calculator = 9420 cube units
Last edited on
Chervil, prototype declarations are now correct, aren't they?
No, you gave len 10 and hei as 30. Yet you call the function with rad and hei, what value does rad contain?
So sorry.. I got it..My bad. Thank you so much jlb!!
Updated code:
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
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void volume(float);
void volume(float,float,float);
void volume(float,float);
int ch;
float len,bre,hei,rad;
char ans;
do
{
cout<<"\nVOLUME MENU";
cout<<"\n\t1. Cube\n\t2. Cuboid\n\t3. Cylinder";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter length of cube:";
cin>>len;
volume(len);
break;
case 2:cout<<"Enter length x breadth x height of cuboid:";
cin>>len>>bre>>hei;
volume(len,bre,hei);
break;
case 3:cout<<"Enter radius x height of cylinder:";
cin>>rad>>hei;
volume(rad,hei);
break;
default:cout<<"Wrong Input!";
}
cout<<"\nDo you want to continue? (Y/N):";
cin>>ans;
}while (ans=='y'||ans=='Y');
getch();
}
void volume(float l)
{
cout<<"Volume of cube = "<<(l*l*l)<<" cube units";
}
void volume(float l,float b,float h)
{
cout<<"Volume of cuboid = "<<(l*b*h)<<" cube units";
}
void volume(float r,float h)
{
cout<<"Volume of cylinder = "<<(3.14*r*r*h)<<" cube units";
}


Thank you cplusplus.com!
Do you know that proper indentation and a little vertical and horizontal white space would make you program much easier to read. With proper indentation and adequate white space you may have been able to see some of the problems with your code. Another thing that would probably help would be to use meaningful variable names, and avoid single letter names, especially l,o,I,Q,O.

And you really should move your function prototypes outside of main(), a main() that should be defined to return an int, not a void.

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
#include <iostream.h>
#include <conio.h>

void volume(float);
void volume(float, float, float);
void volume(float, float);

int main()
{
   clrscr();
   int ch;
   float len, bre, hei, rad;
   char ans;
   
   do
   {
      cout << "\nVOLUME MENU";
      cout << "\n\t1. Cube\n\t2. Cuboid\n\t3. Cylinder";
      cout << "\nEnter your choice:";
      cin >> ch;
      
      switch(ch)
      {
         case 1:
            cout << "Enter length of cube:";
            cin >> len;
            volume(len);
            break;
         case 2:
            cout << "Enter length x breadth x height of cuboid:";
            cin >> len >> bre >> hei;
            volume(len, bre, hei);
            break;
         case 3:
            cout << "Enter radius x height of cylinder:";
            cin >> rad >> hei;
            volume(rad, hei);
            break;
         default:
            cout << "Wrong Input!";
      }
      cout << "\nDo you want to continue? (Y/N):";
      cin >> ans;
   }  while (ans == 'y'|| ans == 'Y');

   getch();

   return 0;
}

void volume(float length)
{
   cout << "Volume of cube = " << (length * length * length) << " cube units";
}

void volume(float length, float bre, float height)
{
   cout << "Volume of cuboid = " << (length * bre * height) << " cube units";
}

void volume(float radius, float height)
{
   cout << "Volume of cylinder = " << (3.14 * radius * radius * height) << " cube units";
}


Lastly you need to really consider getting a newer compiler, the compiler you're currently is very very outdated.

jlb, actually, it's taught in our school like that. we have always done c++ with void main(). doesn't make much difference, does it? int needs return, void needs nothing.
doesn't make much difference, does it?
Actually it does. void main() is not valid in either or C++. Your school is teaching you how to use incorrect code. It may happen to be supported by your particular complier, but if you were to use a different compiler, chances are it would simply fail to compile.
jlb, actually, it's taught in our school like that. we have always done c++ with void main(). doesn't make much difference, does it?


It's the difference between a conforming program and one that isn't. One should strive to avoid the undefined behavior one can encounter in non-conforming code.

int needs return, void needs nothing.

In C++, not explicitly returning from main is the same as returning 0 (provided the return type is int of course,) so that point is entirely moot.
uh well, okay. I'm using Turbo C++. Can someone tell me a good compiler?
There are some suggestions for compilers on this page here at the bottom
http://www.cplusplus.com/doc/tutorial/introduction/

Topic archived. No new replies allowed.