Author Topic: C++ Geometry Calculator  (Read 4867 times)

0 Members and 1 Guest are viewing this topic.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
C++ Geometry Calculator
« on: 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?


Code: [Select]

/*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;
}
The Bigger they are...The more likely you'll get your ass kicked

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: C++ Geometry Calculator
« Reply #1 on: September 17, 2012, 06:16:19 am »
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.
"Always have a plan"

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: C++ Geometry Calculator
« Reply #2 on: September 17, 2012, 06:42:49 am »
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?
The Bigger they are...The more likely you'll get your ass kicked

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: C++ Geometry Calculator
« Reply #3 on: September 17, 2012, 06:55:01 am »
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.
« Last Edit: September 17, 2012, 04:46:59 pm by p_2001 »
"Always have a plan"

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: C++ Geometry Calculator
« Reply #4 on: September 22, 2012, 05:57:27 pm »

Haven't u heard of functions. You should use functions.
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: C++ Geometry Calculator
« Reply #5 on: September 22, 2012, 11:58:15 pm »
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:
Code: [Select]
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! :)
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: C++ Geometry Calculator
« Reply #6 on: September 23, 2012, 02:06:55 am »
I have to add, functions doesn't mean it's OOP. Just saying.
>>>import this
-----------------------------