EvilZone
Programming and Scripting => C - C++ => : Super_mario666 September 17, 2012, 05:12:42 AM
-
this is a bit of homework i had to do for class. any ideas on how to make it better?
/*Geometry Calculator
------------------------------------------------------------------------------------------------------------------------*/
#include <iostream>
#include <math.h>
using namespace std;
int main()
{ int choice; //The users choice from the Main Menu
int radius; // Radius of the circle
double circle; // Area of the circle
int length; // Length of the Rectangle
int width; //Width of the Rectangle
int rectangle; //Area of the Rectangle
int base; // Base of Triangle
double triangle; // Area of Triangle
int height; // Height of triangle
/*---------------Main Menu----------------------*/
cout << "Welcome to the Geometry Calculator!!!!\n";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "Please choose from the following operations\n";
cout << "\n1. Calculate the Area of a Circle \n2. Calculate the Area of a Rectangle \n";
cout << "3. Calculate the Area of a Triangle \n4. Quit\n";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\nEnter your choice (1-4): ";
cin >> choice;
/*-----------------------------------------------*/
while (choice <=0 || choice > 4) //User choice validation
{
cout << "Please choose from the following choices. /n";
cin >> choice;
}
switch (choice)
{
case 1: // Area of a Circle
cout << "Please enter the Radius of the circle: ";
cin >> radius;
while (radius < 0)
{
cout << " Please enter a valid number.\n";
cin >> radius;
}
circle = 3.14159 * pow(radius, 2);
cout << "The Area of the Circle is " << circle << endl;
break;
case 2: // Area of a rectangle
cout << "Please enter the length of the Rectangle: ";
cin >> length;
while (length < 0)
{
cout << "Please enter a valid number.\n";
cin >> length;
}
cout << "Please enter the width of the Rectangle: ";
cin >> width;
while (width < 0)
{
cout << "Please enter a valid number.\n";
cin >> width;
}
rectangle = length * width;
cout << "The Area of the Rectangle is " << rectangle << endl;
break;
case 3: // Area of a triangle
cout << "Please enter the Base of the Triangle: ";
cin >> base;
while (base < 0)
{
cout << "Please enter a valid number.\n";
cin >> base;
}
cout << "Please enter the height of the Triangle: ";
cin >> height;
while (height < 0)
{
cout << "Please enter a valid number.\n";
cin >> height;
}
triangle = base * height * .5;
cout << "The Area of the Triangle is " << triangle << endl;
break;
case 4:// Quit
break;
}
system ("pause");
return 0;
}
-
You've used too many variables.. I don't think that is a good practice.
The code is not object oriented....
Define functions and then call them.
You only needed 3 variables.
-
You've used too many variables.. I don't think that is a good practice.
How so?
The code is not object oriented....
Define functions and then call them.
You only needed 3 variables.
hmm yea i guess could of done that i just had a stream of consciousness thing going here.
is there any consequences from doing that?
-
How so?
hmm yea i guess could of done that i just had a stream of consciousness thing going here.
is there any consequences from doing that?
Initially when memory used to be low... you can understand.
Now, if you use to many variables in big programs.. Think about the wasted space.
It is good practice to use less variables.
You just needed 2 global variables to act as parameters for your functions.
Then pass then to the functions defined.
When the code is compiled 6 int means 6 different memory address. 2 means just two.
As for object oriented, the more modules the better is the program.
Main should be used only for calling various functions/methods
let's say you needed to add more shapes.. Changing main is not the proper way.
You could just define a new function and then add a choice.
Yes, right now it is trivial, but again you're just learning. Better learn the better ways. In bigger programs you will find that making small parts and joining them seems to work better.
-
Haven't u heard of functions. You should use functions.
-
Haven't u heard of functions. You should use functions.
I lol'd at this cause you used functions in your program when they were unnecessary. but your right, a large program like this could use a function to make it more readable and create a "black box" which is just good programming practice. sorry to be replying to this so late OP, but better late than never right?
So heres what I would do:
-Initialize all variables that have to do with numbers, set them to 0 when you declare them
-put everthing you have under case 1, case 2, and case 3 into functions called areaofCircle (or sumethin) that way it will look something like:
case 1:
areaofCircle(values to pass through);
case 2:
areaofRectangle(values);
//so on so forth, much easier on the eyes/brain and makes you look like a better programmer imo ;)
-always have a default in your switch to handle bad input
-wrap the entire program in a while(bool == true) then instead of a pause, ask the user if they would like to use it again. and if so it takes them back to the main menu.
anyways, sorry for the late reply and hope this helps for future projects! :)
-
I have to add, functions doesn't mean it's OOP. Just saying.