trying to return to a selection input request (switch loop)

Hi i have a program that calculates volume of shapes, my problem is that after it has calculated the volume, i want it to return to the selection 'menu' to perform the next calculation or exit the programme, any help would be much appreciated.
(i have left the functions out
for ease of reading)

int main (){
bool exit;
exit = true;
do
{


int selection;
cout<<"This application will help you calculate the volume of a shape\nPlease select a shape:\n(1)Cuboide\n(2)Cylinder\n(3)Cone\n(4)Sphere\n(5)Square based pyramid\n";
cin>> selection;
switch (selection)
{
{
case 1://cuboide

double l,b,h;

cout<<"Please enter the length,breadth,and height of the cuboide\n";
cin>>l;
cin>>b;
cin>>h;
cout<<"Volume:"<<cuboide(l,b,h)<<endl;
system("pause");
return 0;
break;
}
{
case 2://cylinder
{
double r,h;
cout<<"Please enter the radius and height of the cylinder\n";
cin>>r;
cin>>h;
cout<<"Volume:"<<cylinder(r,h)<<endl;
system("pause");
return 0;
break;
}
case 3://cone
{
double r,h;
cout<<"Please enter the radius and height of the cone\n";
cin>>r;
cin>>h;
cout<<"Volume:"<<cone(r,h)<<endl;
system("pause");
return 0;
break;
}
case 4://sphere
{
double r;
cout<<"Please enter the radius of the sphere\n";
cin>>r;
cout<<"Volume:"<<sphere(r)<<endl;
system("pause");
return 0;
break;
}
case 5://squarepyramid
{
double b,h;
cout<<"Please enter the base length and height of the pyramid\n";
cin>>b;
cin>>h;
cout<<"Volume:"<<sqrpyramid(b,h)<<endl;
system("pause");
return 0;
break;
}
default:
{
cout<<"Invalid input\n";
system("pause");
return 0;
}
}


}
} while (exit = true);
}
For example you can declare one of the functions the following way

double cuboide( double length, double breadth, double height );
Last edited on
my apologies maybe i wasnt clear, i have declared the functions and they work correctly, i just didnt copy they into the post to make it shorter and easier to read, my problem is that i would like the program to repeat this part of the loop

int selection;
cout<<"This application will help you calculate the volume of a shape\nPlease select a shape:\n(1)Cuboide\n(2)Cylinder\n(3)Cone\n(4)Sphere\n(5)Square based pyramid\n";
cin>> selection;
switch (selection)

after it has displayed the volume of the previous shape
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int selection;

do
{
   cout << "This application will help you calculate the volume of a shape\n"
                "Please select a shape:\n"
                "(1)Cuboide\n"
                "(2)Cylinder\n"
                "(3)Cone\n"
                "(4)Sphere\n"
                "(5)Square based pyramid\n"
                "(6)Exit\n";
   cin>> selection;
   switch (selection)
   {
   // switch stuff
   }
} while ( selection != 6 );


EDIT: And indeed as @cire pointed out remove return statements within the switch.
Last edited on
my problem is that i would like the program to repeat this part of the loop

Then remove the returns from within the loop, so that it actually loops.
Last edited on
thanks, i was muddling in the right direction it seems, much appreciated.
i eventually added
"Please enter ...... (0)Exit"
while (selection !=0)

i think my programe now exceeds my assignments requirments :P , but now id like to clear the screen after every iteration of the loop

how would i do this?

EDIT: the returns in the loop was a stupid idea/mistake as a result of frustration! and the solution always seems obvious when you see it!
Last edited on
Try system( "cls" );
Topic archived. No new replies allowed.