how do i end a function

if it is 0 or negative i want the user to input the shape again



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
//enter shape--------------------------
	int choose;
	cout << "Select a shape: " << endl << "1. sphere " << endl << "2. cube "     << endl << "3. cylinder"  << endl << "Enter number: ";
	cin >> choose;
	


//-----------one of my switch----------------
case 2 : box.calcvolc(sidez); break;

 
//function-------------------------------------
void labf::calcvolc(double side)
{
	cout << "Enter the side of a cube: ";
 	cin >> side;
	while(side <= 0)
 	{
		cout << "Negative numbers or 0 do not give a volume.";
	}
	cout << endl;
	
	volumec = pow(side,3);
	cout << "The volume of the cube is " << volumec << endl;
}
Line 16 needs to appear after line 19 (within the loop) as well in order to change side.

Notice that changing side will not have any effect outside the function calcvolc(...).
you can end a function at any time with the return statement.
if the function has a value, you must return a value (int foo needs return(number);) while void functions just an empty return works (return;)

You can either loop with a check (if user put in bad stuff, ask for them to enter it again until it is valid, in a loop) or you can exit the function and call the function again in a loop depending on how you want to set that up.
Topic archived. No new replies allowed.