#include <iostream>
#include <iomanip>
usingnamespace std;
class cube
{
public:
double side;
double volume()
{
return (side*side*side);
}
cube(){}
~cube(){}
};
class sphere
{
public:
double radius;
double volume()
{
return ((4*3.141593*radius)/3);
}
sphere(){}
~sphere(){}
};
int main()
{
int Menu_choice;
cout << "Please select what solid you would like the volume of\n";
cout << " For a cube, press 1\n";
cout << "For a sphere press 2\n";
cout << "To end the program, press x\n";
cin >> Menu_choice;
switch(Menu_choice)
{
case 1:
cube c1;
cout << "Please enter the length of the side of the cube\n";
cin >> c1.side;
cout << "The volume of the cube is :" << c1.volume << endl;
break;
case 2:
sphere s1;
cout << "Please enter the radius of the sphere\n";
cin >> s1.radius;
cout << "The volume of the sphere is: " << s1.volume << endl;
break;
case 120:
case 88:
break;
casedefault:
cout << "Please enter a valid choice.\n";
}
return 0;
}
The error is in line 44 (and 50), not 42. volume is a function, but you aren't calling it.
You can't jump over variable initializations, like the error message says. If Menu_choice was 2 or something else, you'd jump over the initialization of c1, which isn't allowed. So you have to make sure c1 is destroyed before case 2 starts by moving it into a subblock:
1 2 3 4 5 6 7 8 9
case 1:
{
cube c1;
cout << "Please enter the length of the side of the cube\n";
cin >> c1.side;
cout << "The volume of the cube is :" << c1.volume() << endl;
break;
}
case 2:
#include <iostream>
#include <iomanip>
usingnamespace std;
class cube
{
public:
double side;
double volume1(void)
{
return (side*side*side);
}
cube(){}
~cube(){}
};
class sphere
{
public:
double radius;
double volume1(void)
{
return ((4*3.141593*radius)/3);
}
sphere(){}
~sphere(){}
};
int main()
{
int Menu_choice;
cout << "Please select what solid you would like the volume of\n";
cout << " For a cube, press 1\n";
cout << "For a sphere press 2\n";
cout << "To end the program, press x\n";
cin >> Menu_choice;
switch(Menu_choice)
{
case 1:
{
cube c1;
cout << "Please enter the length of the side of the cube\n";
cin >> c1.side;
cout << "The volume of the cube is :" << c1.volume1() << endl;
}
break;
case 2:
{
sphere s1;
cout << "Please enter the radius of the sphere\n";
cin >> s1.radius;
cout <<"The volume of the sphere is: "<< s1.volume1()<<endl;
}
break;
case 120:
case 88:
break;
default:
cout << "Please enter a valid choice.\n";
}
return 0;
}
switch(Menu_choice)
{
case 1:
{
cube c1;
cout << "Please enter the length of the side of the cube\n";
cin >> c1.side;
cout << "The volume of the cube is :" << c1.volume() << endl;
break;
}
case 2:
{
sphere s1;
cout << "Please enter the radius of the sphere\n";
cin >> s1.radius;
cout << "The volume of the sphere is: " << s1.volume() << endl;
break;
}
case 120:
{
break;
}
case 88:
{
break;
}
casedefault:
cout << "Please enter a valid choice.\n";
}
return 0;
}