Jump to content
xisto Community
Simba49

My First C++ Program: Area, Volume Etc. Calculator My first program

Recommended Posts

//This program calculates the perimeter, area, surface area, or volume of any shape or solid.#include <iostream>#include <string>#define PI 3.14159265359using namespace std;float psquare (float a){return (4*a);}float prectangle (float w, float l){return ((2*w)+(2*l));}float pparallelogram (float b, float h){return ((2*b)+(2*h));}float ptrapezoid (float s, float t, float b){return ((2*s)+b+t);}float pcircle (float r){return (PI*r*2);}float pellipse (float r, float s){return (PI*(r+s));}float ptriangle (float a, float b, float c){return (a+b+c);}float asquare (float a){return (a*a);}float arectangle (float l, float w){return (l*w);}float aparallelogram (float b, float h){return (h*b);}float atrapezoid (float t, float b, float h){return (h/2*(t+b));}float acircle (float r){return (PI*r*r);}float aellipse (float r, float s){return (PI*r*s);}float atriangle (float b, float h){return (b*h/2);}float scube (float a){return (a*a*6);}float vcube (float a){return (a*a*a);}float scylinder (float r, float h){return ((2*PI*r*h)+(2*PI*(r*r)));}float vcylinder (float r, float h){return (PI*(r*r)*h);}float scone (float r, float s){return ((s*PI*r)+(PI*(r*r)));}float vcone (float r, float h){return (1/3*PI*(r*r)*h);}float ssphere (float r){return (4*PI*(r*r));}float vsphere (float r){return (4/3*PI*(r*r*r));}float sprism (float B, float P, float h){return ((2*B)+(P*h));}float vprism (float B, float h){return (B*h);}float spyramid (float B, float s, float l, float n){return (B+n*(1/2*s*l));}float vpyramid (float B, float h){return (1/3*B*h);}float ppentagon (float a){return (5*a);}float phexagon (float a){return (6*a);}float apentagon (float a, float s){return (a*s*5/2);}float ahexagon (float a, float s){return (a*s*6/2);}int main () {	dimension:	string d, s, t, p;	float x, y, z, h, perimeter, area, average;	char n;	cout <<"Is your shape 2D or 3D?:";	cin >> d;		if ( (d == "2d") || (d == "2D") || (d == "2") )			{		type:		cout <<"Are you determining (perimeter or area)?:";		cin >> t;		if ( (t == "perimeter") || (t == "Perimeter") || (t == "p") || (t == "P") ) {t="perimeter";} else if ( (t == "Area") || (t == "area") || (t == "a") || (t == "A") ) {t="area";}		if ( (t == "perimeter") || (t == "area") )		{			shape:			cout <<"What shape is it \n(square, rectangle, parallelogram, trapezoid, circle, ellipse, triangle)?:";			cin >> s;			if ( (s == "Square") || (s == "square") || (s == "s") || (s == "S") ) {s="square";} else if ( (s == "Rectangle") || (s == "rectangle") || (s == "R") || (s == "r") ) {s="rectangle";} else if ( (s == "parallelogram") || (s == "Parallelogram") || (s == "p") || (s == "P") ) {s="parallelogram";} else if ( (s == "trapezoid") || (s == "Trapezoid") || (s == "trap") || (s == "Trap") ) {s="trapezoid";} else if ( (s == "Circle") || (s == "circle") || (s == "C") || (s == "c") ) {s="circle";} else if ( (s == "Ellipse") || (s == "ellipse") || (s == "E") || (s == "e") ) {s="ellipse";} else if ( (s == "triangle") || (s == "Triangle") || (s == "t") || (s == "T") ) {s="triangle";}			if ( (s == "square") || (s == "rectangle") || (s == "parallelogram") || (s == "trapezoid") || (s == "circle") || (s == "ellipse") || (s == "triangle") )			{				if (s == "square")					{ goto square;				} else if (s == "rectangle")					{ goto rectangle;				} else if (s == "parallelogram")					{ goto parallelogram;				} else if (s == "trapezoid")					{ goto trapezoid;				} else if (s == "circle")					{ goto circle;				} else if (s == "ellipse")					{ goto ellipse;				} else if (s == "triangle")					{ goto triangle;				}square:				if (t == "perimeter")				{				cout <<"What is the length of a side?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << psquare (x) << ".\n";				goto end;				}				else if (t == "area")					{				cout <<"What is the length of a side?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << asquare (x) << ".\n";				goto end;				}rectangle:				if (t == "perimeter")				{				cout <<"What is the length of the rectangle?:";				cin >> x;				cout <<"\nWhat is the width of the rectangle?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << prectangle (x,y) << ".\n";				goto end;				}				else if (t == "area")					{				cout <<"What is the length of the rectangle?:";				cin >> x;				cout <<"\nWhat is the width of the rectangle?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << arectangle (x,y) << ".\n";				goto end;				}parallelogram:					if (t == "perimeter")				{				cout <<"What is the base of the parallelogram?:";				cin >> x;				cout <<"\nWhat is the height of the parallelogram?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << pparallelogram (x,y) << ".\n";				goto end;				}				else if (t == "area")					{				cout <<"What is the base of the parallelogram?:";				cin >> x;				cout <<"\nWhat is the height of the parallelogram?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << aparallelogram (x,y) << ".\n";				goto end;				}trapezoid:				if (t == "perimeter")				{				cout <<"What is the length of the top?:";				cin >> y;				cout <<"\nWhat is the length of the bottom?:";				cin >> z;				cout <<"\nWhat is the length of th side?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << ptrapezoid (x, y, z) << ".\n";				goto end;				}				else if (t == "area")					{				cout <<"What is the length of top?:";				cin >> x;				cout <<"\nWhat is the length of bottom?:";				cin >> y;				cout <<"\nWhat is the height of the trapezoid?:";				cin >> z;				cout <<"The " << t << " of the " << s << " is " << atrapezoid (x, y, z) << ".\n";				goto end;				}circle:				if (t == "perimeter")				{				cout <<"What is the radius of the circle?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << pcircle (x) << ".\n";				goto end;				}				else if (t == "area")					{				cout <<"What is the radius of the circle?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << acircle (x) << ".\n";				goto end;				}ellipse:				if (t == "perimeter")				{				cout <<"What is the first radius?:";				cin >> x;				cout <<"\nWhat is the second radius?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is aproximately " << pellipse (x,y) << ".\n";				goto end;				}				else if (t == "area")					{				cout <<"What is the first radius?:";				cin >> x;				cout <<"\nWhat is the second radius?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << aellipse (x,y) << ".\n";				goto end;				}triangle:					if (t == "perimeter")				{				cout <<"What is the length of side1?:";				cin >> y;				cout <<"\nWhat is the length of side2?:";				cin >> z;				cout <<"\nWhat is the length of side3?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << ptriangle (x, y, z) << ".\n";				goto end;				}				else if (t == "area")					{				cout <<"What is the base of the triangle?:";				cin >> x;				cout <<"\nWhat is the height of the triangle?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << atriangle (x, y) << ".\n";				goto end;				}			}			else {				goto shape;			}		}		else {			goto type;		}	} else if ( (d == "3d") || (d == "3D") || (d == "3") )			{		typ:		cout <<"Are you determining (surfacearea or volume)?:";		cin >> t;		if ( (t == "surfacearea") || (t == "Surfacearea") || (t == "surface") || (t == "Surface") || (t == "s") || (t == "S") ) {t="surface area";} else if ( (t == "Volume") || (t == "volume") || (t == "v") || (t == "V") ) {t="volume";}		if ( (t == "surface area") || (t == "volume") )		{			shap:			cout <<"What shape is it \n(cube, prism, cylinder, pyramid, cone, sphere)?:";			cin >> s;			if ( (s == "Cube") || (s == "cube") || (s == "c") || (s == "C") ) {s="cube";} else if ( (s == "Prism") || (s == "prism") || (s == "P") || (s == "p") ) {s="prism";} else if ( (s == "cylinder") || (s == "Cylinder") || (s == "cy") || (s == "CY") ) {s="cylinder";} else if ( (s == "pyramid") || (s == "Pyramid") || (s == "PY") || (s == "py") ) {s="pyramid";} else if ( (s == "Cone") || (s == "cone") || (s == "CO") || (s == "co") ) {s="cone";} else if ( (s == "Sphere") || (s == "sphere") || (s == "S") || (s == "s") ) {s="sphere";} 			if ( (s == "cube") || (s == "prism") || (s == "cylinder") || (s == "pyramid") || (s == "cone") || (s == "sphere") )			{				if (s == "cube")					{ goto cube;				} else if (s == "prism")					{ goto prism;				} else if (s == "cylinder")					{ goto cylinder;				} else if (s == "pyramid")					{ goto pyramid;				} else if (s == "cone")					{ goto cone;				} else if (s == "sphere")					{ goto sphere;				} cube:				if (t == "surface area")				{				cout <<"What is the length of a side?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << scube (x) << ".\n";				goto end;				}				else if (t == "volume")					{				cout <<"What is the length of a side?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << vcube (x) << ".\n";				goto end;				}cylinder:				if (t == "surface area")				{				cout <<"What is the radius of the base?:";				cin >> x;				cout <<"\nWhat is the height of the cylinder?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << scylinder (x, y)   << ".\n";				goto end;				}				else if (t == "volume")					{				cout <<"What is the radius of the base?:";				cin >> x;				cout <<"\nWhat is the height of the cylinder?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << vcylinder (x, y)   << ".\n";				goto end;				}cone:				if (t == "surface area")				{				cout <<"What is the radius of the base?:";				cin >> x;				cout <<"\nWhat is the slant length of the cone?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << scone (x, y)   << ".\n";				goto end;				}				else if (t == "volume")					{				cout <<"What is the radius of the base?:";				cin >> x;				cout <<"\nWhat is the height of the cone?:";				cin >> y;				cout <<"The " << t << " of the " << s << " is " << vcone (x, y)   << ".\n";				goto end;				}sphere:				if (t == "surface area")				{				cout <<"What is the radius of the sphere?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << ssphere (x) << ".\n";				goto end;				}				else if (t == "volume")					{				cout <<"What is the radius of the sphere?:";				cin >> x;				cout <<"The " << t << " of the " << s << " is " << vsphere (x) << ".\n";				goto end;				}prism:				cout <<"What shape is the face of the " << s  <<"?\n";				cout <<"(triangle, rectagle, pentagon, hexagon):";				cin >> p;				if ( (p == "triangle") || (p == "Triangle") || (p == "t") || (p == "T") ) {p="triangular";} else if ( (p == "Rectangular") || (p == "rectangular") || (p == "R") || (p == "r") ) {p="rectangular";} else if ( (p == "pentagon") || (p == "Pentagon") || (p == "P") || (p == "p") ) {p="pentagonal";} else if ( (p == "hexagon") || (p == "Hexagon") || (p == "H") || (p == "h") ) {p ="hexagonal";} 				if ( (p == "triangular") || (p == "rectangular") || (p == "pentagonal") || (p == "hexagonal") )				{					if ((t == "surface area") && (p == "triangular"))					{						cout <<"What is the length of side1 of the triangle?:";						cin >> y;						cout <<"\nWhat is the length of side2 of the triangle?:";						cin >> z;						cout <<"\nWhat is the length of side3 of the triangle?:";						cin >> x;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						perimeter = ptriangle (x, y, z);						cout <<"What is the base of the triangle?:";						cin >> x;						cout <<"\nWhat is the height of the triangle?:";						cin >> y;						area = atriangle (x, y);						cout <<"The " << t << " of the " << p << " " << s << " is " << sprism (area, perimeter, h)  << ".\n";						goto end;					}					else if ((t == "surface area") && (p == "rectangular"))					{																cout <<"What is the length of the rectangle?:";						cin >> x;						cout <<"\nWhat is the width of the rectangle?:";						cin >> y;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						perimeter = prectangle (y, x);						area = arectangle (x , y);						cout <<"The " << t << " of the " << p << " " << s << " is " << sprism (area, perimeter, h)  << ".\n";						goto end;					}					else if ((t == "surface area") && (p == "pentagonal"))					{						cout <<"What is the length of a side on the pentagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the pentagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						perimeter = ppentagon (x);						area = apentagon (y, x);						cout <<"The " << t << " of the " << p << " " << s << " is " << sprism (area, perimeter, h)  << ".\n";						goto end;					}					else if ((t == "surface area") && (p == "hexagonal"))					{						cout <<"What is the length of a side on the hexagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the hexagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						perimeter = phexagon (x);						area = ahexagon (y, x);						cout <<"The " << t << " of the " << p << " " << s << " is " << sprism (area, perimeter, h)  << ".\n";						goto end;					}																				else if ((t == "volume") && (p == "triangular"))					{											cout <<"What is the base of the triangle?:";						cin >> x;						cout <<"\nWhat is the height of the triangle?:";						cin >> y;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						area = atriangle (x, y);						cout <<"The " << t << " of the " << p << " " << s << " is " << vprism (area, h)  << ".\n";						goto end;					}					else if ((t == "volume") && (p == "rectangular"))					{																cout <<"What is the length of the rectangle?:";						cin >> x;						cout <<"\nWhat is the width of the rectangle?:";						cin >> y;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						area = arectangle (x , y);						cout <<"The " << t << " of the " << p << " " << s << " is " << vprism (area, h)  << ".\n";						goto end;					}					else if ((t == "volume") && (p == "pentagonal"))					{						cout <<"What is the length of a side on the pentagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the pentagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						area = apentagon (y, x);						cout <<"The " << t << " of the " << p << " " << s << " is " << vprism (area, h)  << ".\n";						goto end;					}					else if ((t == "volume") && (p == "hexagonal"))					{						cout <<"What is the length of a side on the hexagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the hexagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the height of the prism?:";						cin >> h;						area = ahexagon (y, x);						cout <<area;						cout <<"The " << t << " of the " << p << " " << s << " is " << vprism (area, h)  << ".\n";						goto end;					} 				} else {					goto prism;				}pyramid:					cout <<"What shape is the face of the " << s  <<"?\n";				cout <<"(triangle, rectagle, pentagon, hexagon):";				cin >> p;				if ( (p == "triangle") || (p == "Triangle") || (p == "t") || (p == "T") ) {p="triangular";} else if ( (p == "Rectangular") || (p == "rectangular") || (p == "R") || (p == "r") ) {p="rectangular";} else if ( (p == "pentagon") || (p == "Pentagon") || (p == "P") || (p == "p") ) {p="pentagonal";} else if ( (p == "hexagon") || (p == "Hexagon") || (p == "H") || (p == "h") ) {p ="hexagonal";} 				if ( (p == "triangular") || (p == "rectangular") || (p == "pentagonal") || (p == "hexagonal") )				{					if ((t == "surface area") && (p == "triangular"))					{												cout <<"\nWhat is the slant length of the pyramid?:";						cin >> x;						cout <<"What is the base of the triangle?:";						cin >> y;						cout <<"\nWhat is the height of the triangle?:";						cin >> z;						area = atriangle (x, y);						cout <<"The " << t << " of the " << p << " " << s << " is " << spyramid (area, y, x, 3) << ".\n";						goto end;					}					else if ((t == "surface area") && (p == "rectangular"))					{																cout <<"What is the length of the rectangle?:";						cin >> x;						cout <<"\nWhat is the width of the rectangle?:";						cin >> y;						cout <<"\nWhat is the slant length of the pyramid?:";						cin >> z;						average = (x+y)/2;						area = arectangle (x , y);						cout <<"The " << t << " of the " << p << " " << s << " is " << spyramid (area, average,z,4)  << ".\n";						goto end;					}					else if ((t == "surface area") && (p == "pentagonal"))					{						cout <<"What is the length of a side on the pentagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the pentagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the slant length of the pyramid?:";						cin >> z;						area = apentagon (y, x);						cout <<"The " << t << " of the " << p << " " << s << " is " << spyramid (area, x, z, 5)   << ".\n";						goto end;					}					else if ((t == "surface area") && (p == "hexagonal"))					{						cout <<"What is the length of a side on the hexagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the hexagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the slant length of the pyramid?:";						cin >> z;						perimeter = phexagon (x);						area = ahexagon (y, x);						cout <<"The " << t << " of the " << p << " " << s << " is " << spyramid (area, x, z, 5)   << ".\n";						goto end;					}																				else if ((t == "volume") && (p == "triangular"))					{											cout <<"What is the base of the triangle?:";						cin >> x;						cout <<"\nWhat is the height of the triangle?:";						cin >> y;						cout <<"\nWhat is the height of the pyramid?:";						cin >> h;						area = atriangle (x, y);						cout <<"The " << t << " of the " << p << " " << s << " is " << vpyramid (area, h)  << ".\n";						goto end;					}					else if ((t == "volume") && (p == "rectangular"))					{																cout <<"What is the length of the rectangle?:";						cin >> x;						cout <<"\nWhat is the width of the rectangle?:";						cin >> y;						cout <<"\nWhat is the height of the pyramid?:";						cin >> h;						area = arectangle (x , y);						cout <<"The " << t << " of the " << p << " " << s << " is " << vpyramid (area, h)  << ".\n";						goto end;					}					else if ((t == "volume") && (p == "pentagonal"))					{						cout <<"What is the length of a side on the pentagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the pentagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the height of the pyramid?:";						cin >> h;						area = apentagon (y, x);						cout <<"The " << t << " of the " << p << " " << s << " is " << vpyramid (area, h)  << ".\n";						goto end;					}					else if ((t == "volume") && (p == "hexagonal"))					{						cout <<"What is the length of a side on the hexagon?:";						cin >> x;						cout <<"\nWhat is the measurement from the center of the hexagon to a side (apothem)?:";						cin >> y;						cout <<"\nWhat is the height of the pyramid?:";						cin >> h;						area = ahexagon (y, x);						cout <<"The " << t << " of the " << p << " " << s << " is " << vpyramid (area, h)  << ".\n";						goto end;					} 				} else {					goto pyramid;				}			}			else {				goto shap;			}		}		else {			goto typ;		}	}	else {		goto dimension;	}end:	cout <<"Do you want to calculate an other shape? (y / n):";		cin >>n;	if (n == 'y')	{ goto dimension; }	return 0;}

Share this post


Link to post
Share on other sites

I agree with the above statement. While GOTO statements can be very useful, they aren't appreciated by full-fledged programmers. As a matter a fact, mine programming teacher has forbidden us to use them during one of the first classes :PAs for the rest of the program, it is far to complicated. With all those IF statements you leave the impression of the program not having a mind of its own. What I am trying to say is that it is possible to create such a program that would deal with the same problem, yet with far less lines of code. I do not claim I could do it, but I am absolutely sure it is possible with all C++ has to offer :P

Share this post


Link to post
Share on other sites

Wow, yea, as mentioned goto is bad practice. There are a LOT of ways goto can make programs mess up, especially if you ever try to modify code with numerous goto statements. Further you are not taking advantage of the language whatsoever. Write functions to handle things and call them instead of goto statements. Control the user input (tell them what to type) rather then taking into account a shitload of possible string entires (or run the input through the lowercase built in function then take a substring of the first however many letters, and base your assumption off that). Those huge if blocks with tons of or's in it make me cry inside hehe.You just have SO MUCH code for something that could, as mentioned, be done in a much much much more concise way. Ignoring the fact that most of the functions for this are probably built into c++ even wanting to write your own it could be done in much less code and much cleaner code.Another tip would be to take all the user input at once, for example get them to enter the length, width, and height as a comma delimited string or just one at a time, hitting enter in between, but list the order beforehand so you dont need to print/input/print/input/print/input every time.

Share this post


Link to post
Share on other sites

No need to blame this guy, ^_^ remember this is his very first app, so if it does work, it's very good.Ofc Symba, my eyes were bleeding too when i saw those goto's, it made me remember of those old DOS Batch scripts...A switch would be appreciated too, it's more - beautiful - and i think also slightly more efficient...Gl in your new C++ world!

Share this post


Link to post
Share on other sites
c++ programMy First C++ Program: Area, Volume Etc. Calculator

I cant write a program that will ask the user to input a capital charachter (alphabit) which will allow him chose from the following menue

1- draw an empty squrae shape out of the input charachter of side(S) 

2-output all the capital letter less than the input letter

Please help me solve it as soon as possibale coz I need it today to submitt.

thank,

best regards.

-question by akram

Share this post


Link to post
Share on other sites

It's not bad for a first time, tough I do have some suggestion :)

1) don't ask the user to input words like square, circle, ... Give then a numbered list with options and ask them for a number, makes it a lot more easy to figure out what the user wants.
2) As mentioned very often, don't use GOTO statements. They might look easy to use, but they are messy and they are slow as they need a lot more instructions to handle. It's better to use switch/case or a nested if-else construction.
3) Mind your casings: http://vcf-online.org/docs/ref_manual/ch01s07.html%23id4750221
so

float psquare (float a)
should be
float pSquare (float a)

4) Also very important are the names you give to functions. The name of a function should tell everything it does. "vcylinder" tells nothing whereas "calculateVolumeCylinder" tells everything.
5) Take a look at how you can make classes. This is the power of c++ (as it is an object oriented programming language).
Now you've made the first steps and now that you have all the functions, it's not a bad idea to group them in classes.
So you could say
double radius = 5;myCircle c = new myCircle(radius);cout << c.getVolume();

Keep up the good work and don't give up :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.