Assignment 3: Geometry Helper

Your task for this assignment is to create a “geometry helper” program that calculates the area and perimeter of some geometric shapes. Your program should ask the user to type the name of a shape, and then ask them to enter the necessary information to calculate that shape’s perimeter and area.

Here’s a sample run of how your program should work when it’s done:

+---------------------------------+
| Welcome to the Geometry Helper! |
+---------------------------------+

 Enter the name of a geometric shape (or "done" or "quit"): circle
 What is the radius of your circle? 4.3

 It's area is 58.088
 It's perimeter is 27.0177

 Enter the name of a geometric shape (or "done" or "quit"): rectangle
 What is the width of the rectangle? 5.1
 What is the height of the rectangle? 2

 It's area is 10.2
 It's perimeter is 14.2

 Enter the name of a geometric shape (or "done" or "quit"): squAre
 What is the side length of your square? 4

 It's area is 16
 It's perimeter is 16

 Enter the name of a geometric shape (or "done" or "quit"): octagon
 Sorry, I don't know what a octagon is.

 Enter the name of a geometric shape (or "done" or "quit"): square
 What is the side length of your square? -43
 Oops, you can only enter positive numbers!

 What is the side length of your square? 0
 Oops, you can only enter positive numbers!

 What is the side length of your square? 43

 It's area is 1849
 It's perimeter is 172

 Enter the name of a geometric shape (or "done" or "quit"): done

Your program should be able to handle circles, squares, rectangles, and triangles. To name the shape to process, the user can type:

  • “circle”, or “cir”, for a circle
  • “square”, or “sq”, for a square
  • “rectangle”, or “rect”, for a rectangle
  • “triangle””, or “tri”, for a triangle

In addition, to end the program, the user can type either “quit”, or “done”.

The case of the letters in the input doesn’t matter. So, for example, if the CIR, Circle, and CIrClE all be treated as if the user entered circle.

If the user enters something that is not the name of a shape known by the program, or is not “quit” or “done”, then the program should print a friendly error message, as in the above sample run, and re-ask the user.

Required Functions

An important part of this assignment is to practice creating and using functions. In particular, you should create the following functions:

  • A function called get_positive_num(prompt) that returns (not prints!) a positive double entered by the user. The input prompt is a string that is the message that will be displayed to the user (so they know what to type).

    Here’s a fragment of code showing how get_positive_num(prompt) can be used:

    double height = get_positive_num("How tall are you? ");
    cout << height << " is pretty tall, for a human\n";
    

    Here’s three separate sample runs of this code:

    How tall are you? -5.44
    Oops, you can only enter positive numbers!
    
    How tall are you? 0
    Oops, you can only enter positive numbers!
    
    How tall are you? 5.5
    5.5 is pretty tall, for a human
    

    Importantly, get_positive_num keeps asking the user to enter a number until they enter a positive number. Every time the user enters a non- positive number, get_positive_num prints a friendly and helpful error message, and then re-asks them to enter a valid number.

    You do not need to handle the case where the user enters a string instead of a number. For this assignment, it’s okay in that case if your program crashes.

  • For each shape — circle, square, rectangle, and triangle — write three functions:

    • A function to calculate the shape’s area. This should return a double.
    • A function to calculate the shape’s perimeter. This should return a double.
    • A function to ask the user for the necessary information to calculate the the shape’s area and perimeter. This should be a void function, and it should use the get_positive_num function from above to get numbers from the user.

    Since there are 4 shapes and 3 functions for each shape, this is 12 functions in total for this part.

Submission

Put your program in a single file called a3.cpp. It should #include only, at most, these files: iostream, error.h, string, and cmath. Do not include any other files.

When it’s ready to be submitted, compress a3.cpp into a zip-file archive called a3_submit.zip by typing the following command in a Linux console window:

$ zip a3_submit.zip a3.cpp

Don’t type the $ — that’s the command prompt. Please do not compress your program in any other way — only .zip!

Include the name of the course (CMPT 130), the semester (Fall 2014), and your name and student number in comments at the top of the file.

If you received help from any person (other than yourself), book (such as the textbook), website, etc., then you must cite this help in a comment at the top of your program. If you do not include such a comment, we will assume the program is entirely your own work. Not citing help (even if you accidentally forget) is considered academic dishonesty and will be dealt with seriously.

Marking Scheme

The marking scheme for this assignment is on Canvas.

If your program does not compile correctly, your mark for the entire assignment will be 0. You may also receive 0, or a very low mark, if your assignment compiles but runs in some way that is difficult to judge if it works correctly.