• Log in or sign up for an account here: https://www.sythe.org/login

My first c++ program

hailmatt69

Member
Joined
Mar 10, 2007
Sythe Gold
0
Messages
85
Location
Canada
I am just learning the basics of c++, and i made this program as my first one. You will see that it is very simple coding tha'ts because I havent learned much yet.:
Code:
//This program calculates the perimeter, area, surface area, or volume of any shape or solid.

#include <iostream>
#include <string>
#define PI 3.14159265359
using 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;
}
 
whats it do? and verry nice :D im attepmting 2 learb C++ atm. ne chance u can help me out m8?
 
Nice dude =)
 
[Pkm]Turtwig;1899762 said:
what do you save it as?

Save it as a .cpp and then compile it using a c++ compiler.


Nice code mate =]
 
wow. Nice code, I never really got into c++
 
Great job with the code so far. I have a big problem with the goto statements, however. Why are you using so many? At a glance, it seems you should look up switch statements or use functions (maybe you will if that chapter comes after control flow in your book, but try to stay away from goto statements).
 
Runescape Gamblingrunescape gambling
Back
Top