adding a menu to my program

here there, im trying to insert a menu into my program, it only need to be a simple one so that the user can select which stage of the program they would like, here is copy of the program, could anyone give me advice/help. cheers.


#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

class designer
{
public:

int ns;
int i;
int j;

float x[100];
float y[100];
float depth;
float L[100];
float volume;
float peri;
float area;
float cx;
float cy;

void printmenu ();
void input_corners ();
void input_coords ();
void input_depths ();
void calc_distance ();
void calc_perimeter ();
void calc_area ();
void calc_volume ();
void calc_cx ();
void calc_cy ();
};

int main()
{

designer function;
function.input_corners ();
function.input_coords ();
function.input_depths ();
function.calc_distance ();
function.calc_perimeter ();
function.calc_area ();
function.calc_volume ();
function.calc_cx ();
function.calc_cy ();

return 0;
}







// input of points
void designer :: input_corners ()
{
cout << "Please enter number of sides" << endl;
cin >> ns;
}

// input co-ordinates
void designer :: input_coords ()
{
for ( i=1; i<= ns; i++ )
{
cout << "Please enter co-ordinates" << endl;
cin >> x[i] >> y[i];
}

for ( j=1; j<= ns; j++ )
{
cout << " The x,y co-ordinates are "<< x[j] << "," << y[j] << endl;
}
}


// input of depth
void designer :: input_depths ()
{
cout << "Please enter the depth of the pool" << endl;
cin >> depth;
}



// calculation of distance
void designer :: calc_distance ()
{
for ( i=1; i<= ns-1; i++ )
{

L[i] = sqrt (((x[i+1] - x[i])*(x[i+1] - x[i])) + ((y[i+1] - y[i])*(y[i+1] - y[i])));

cout << "The distance between point " << i << " and " << i+1 << "is: " << L[i] << endl;

}

if ( i = ns )
{
L[i] = sqrt( (x[i]-(x[1]))*(x[i]-(x[1])) + (y[i]-(y[1]))*(y[i]-(y[1])) );

cout << "The distance between point " << i << " and " << 1 << "is: " << L[i] << endl;
cout << endl;
}
}



// calculation of perimeter
void designer :: calc_perimeter ()
{
peri = 0;
for ( i=1; i<=ns-1; i++ )
{
peri = peri + L[i];
}
if ( i = ns)
peri = peri + (sqrt (((x[i] - x[1])*(x[i] - x[1])) + ((y[i] - y[1])*(y[i] - y[1]))));
cout << "The perimeter of the shape is " << peri << endl;
cout << endl;
}



// calculation of area
void designer :: calc_area ()
{
area = 0;
for ( i=1; i<=ns - 1; i++ )
{
area = area + (0.5*(x[i]*y[i+1]-x[i+1]*y[i]));
}

if ( i = ns )
{
area = area + (0.5*(x[i]*y[1]-x[1]*y[i]));
}


cout << "The area of the shape is " << area << endl;
cout << endl;
}


// calculation of volume
void designer :: calc_volume ()
{
volume = area*depth;

cout << "The volume of the shape is " << volume << endl;
cout << endl;
}



// calculation of cx
void designer :: calc_cx ()
{
cx = 0;
for ( i=1; i<ns; i++ )
{
cx = cx + ((1/(6*area))*(x[i]+x[i+1])*(x[i]*y[i+1]-x[i+1]*y[i]));
}

if ( i = ns )
{
cx = cx + ((1/(6*area))*(x[i]+x[1])*(x[i]*y[1]-x[1]*y[i]));
}

cout << "The centroide of for the x co-ordinate is " << cx << endl;
cout << endl;
}


// calculation of cy
void designer :: calc_cy ()
{
cy = 0;
for ( i=1; i<ns; i++ )
{
cy = cy + ((1/(6*area))*(y[i]+y[i+1])*(x[i]*y[i+1]-x[i+1]*y[i]));
}

if ( i = ns )
{
cy = cy + ((1/(6*area))*(y[i]+y[1])*(x[i]*y[1]-x[1]*y[i]));
}

cout << "The centroide of the y co-ordinate is " << cy << endl;
}


You should start by displaying the menu, reading input, then performing an action based on the input. It's likely you would want to put it in a loop of some kind as well.
yes, I like to put menus in do while loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
do
{

cout << "MENU"  << endl;
cout << "MENU"  << endl;
cout << "MENU"  << endl;
cout << "MENU"  << endl;
cout << "5. Quit" << endl;

if (choice == 1)
...

if (choice == 2)
...


}
while (choice != 5);


under the if statements, you can perform function calls if you want, or just insert into the menu choice itself.
Last edited on
Topic archived. No new replies allowed.