2. prog4.h, functions.cpp
Description:
Your assignment is to write a program for a Magical Creatures
Zookeeper. The zookeeper can house up to 100 magical
creatures. Information about each creature needs will be kept
and manipulated in this program. The user should be
able to load magical creature information from any file he or
she chooses, add creatures manually, delete a creature,
print creature information to either a file or to the screen, or
print a cost analysis of each creature and then the total
cost to house and take care of these creatures.
What’s New in Program 5:
• Structures (structure array, nested structures)
PROGRAM SPECIFICATIONS
Structures: (define all structures in Prog4.h)
You will need to create two structures. One is called Cost.
Cost will have the following members:
• The number of hours it takes to take care of a specific
creature.
• The cost (per hour) of taking care of this creature.
• The cost of food to feed this creature for one week.
• The cost of materials/supplies (grooming, medical) for this
creature for one week
The second structure is called Creatures. Creatures will have
the following members:
• The name of the creature
4. your program will call the deleteCreature function. If the user
chooses option 3, then your program will call the
printCreatures function. If the user chooses option 4, then your
program will call the printStatistics function. If the user
chooses option 5, then your program will ask the user if they
wish to save their creature list to a file. If they choose yes,
then your program should call the saveCreaturesToFile function
and then end. If they choose no, then your program
should just end.
ALL OTHER FUNCTIONS SHOULD BE DEFINED IN
functions.cpp
enterCreatures function
The enterCreatures() function takes two parameters: the number
of creatures currently loaded in the Creatures array
and the Creatures array. The function will return the new
number of creatures. Before trying to add any creatures, this
function should first check to make sure the number of creatures
isn’t already 100. Because if it is, then your program
should not add any creatures, but should instead tell the user
that their zoo is at full capacity and that they are not able
to add creatures. Then the function should end.
Otherwise, your program will display a menu asking the user if
they would like to do one of the following:
1. Load my creatures from a file.
2. Enter one creature manually.
Validate the user’s choice.
If the user chooses option 1, then ask the user what the name of
the file is that they would like to load the creatures
from. Then open their file. Check if the file could open before
5. reading from it.
Read each creature from the file and place them in the Creatures
array, making sure that you increment the number of
creatures each time a creature is added. When you are reading
from the file, everything read in will have to be read in
as a string. Some of the Creature members are strings, so that
won’t be a problem. However, some of the Creature
members are floats. So, this function will have to call the
convertToFloat() function (provided later) in order to convert
the string to a float and then placed in the Creatures array.
Then return the number of creatures at the end of the
function.
If the user chooses option 2, then you will want to start a loop
so that the user can add multiple creatures manually
without returning to the main menu. Ask the user for the
following: (make sure you place each bit of information in the
correct place in the Creatures array)
• NAME:
• DESCRIPTION:
• AVERAGE LENGTH (in feet):
• AVERAGE HEIGHT (in feet):
• LOCATION:
• IS IT A DANGEROUS CREATURE? (y or n)
• How many hours do you spend caring for the [insert creature
name here]?
NUM HOURS:
• What is the cost per hour for caring for the [insert creature
name here]?
6. COST PER HOUR:
• How much money do you spend on food for the [insert
creature name here]?
FOOD COST:
• How much money do you spend on grooming and medical
supplies for the [insert creature name here]?
SUPPLY COST:
Then, increment the number of creatures by one. Then, ask the
user if they want to add another creature. If they do,
then repeat this option. If not, then just return the number of
creatures.
deleteCreature function
The deleteCreature() function has two parameters: the current
number of creatures in the Creatures array and the
Creatures array. This function returns the new number of
creatures.
First, this function will say “The following is a list of all the
creatures you take care of: “ and then it will say the name of
each creature. Then, your program will ask the user
What creature do you wish to remove?
CREATURE NAME:
Your program should then read in the name and place it in a
variable. Then, this function will call the
moveArrayElements function, which will take care of removing
the creature. The moveArrayElements function returns a
Boolean value to tell if the creature was removed of not. Check
8. the Creatures array and the Creatures array. The
printCreatures() function will first display a menu to the user
containing the following options:
• Print Creatures to the Screen.
• Print Creatures to a File.
Validate the user’s choice. If the user chooses option 1, then
you will print out all the creatures in the Creatures array to
the screen in the following format:
Notice that the description does word wrapping. In other
words, each word is not split up – they are kept together.
If the user selects option 2, then your program should ask the
user what is the name of the file that they wish to print
the creatures to. Then, open this file. You do not have to check
for errors for this file because it probably will not yet
exist. Then, write all the information to the file like you did to
the screen if the user had chosen option 1. Example
below:
Then, your program should write “Your creatures have been
written to [insert filename here].”
printStatistics function
The printStatistics() function is a void function and it contains
two parameters: the current number of creatures in the
Creatures array and the Creatures array.
This function should print out the total cost of each creature in
9. a table. To figure out the cost of a creature, use the
formula:
Cost = numHours * costPerHour + foodCost + materialCost
Then, this function should also keep a running total of the total
cost of all the creatures.
The table printed should look like this:
saveCreaturesToFile function
The saveCreaturesToFile() function is a void function and it
contains two parameters: the current number of creatures in
the Creatures array and the Creatures array. The function
should ask the user what the name of the file that they wish
to save their creatures to. The function should then open that
file and write out all the creature data in the following
order:
• Name
• Description
• Avg. Length
• Avg. Height
• Location
• Dangerous
• numHours
• costPerHour
• foodCost
• MaterialCost
There should be no newlines or endlines in the file. All data
should be separated by a pound sign (#) instead of a space.
11. Screen shot of creature.txt:
Screen shot of running Prog4.exe
Screen shot of creat04072014.txt:
Screen shot continued of Prog4.exe:
12. Screenshot of creatInput04072014.txt:
PROGRAM 4 -
Magical Creature Zoo
NAME:
Earned
Points
Possible Points
Detailed Description
5 Compiling / Syntax Errors
Compiles with no errors – 5 points
Does not compile – 0 points
85 Specifications
10 Source/Header Files: (10 points)
• Main function is in its own source file (Prog5.cpp)
• Programmer-defined functions in separate file (functions.cpp)
• #include headers, structure definitions and function
13. prototypes are all in header file (Prog5.h)
5 Creatures Text File (5 points)
• Submitted a text file with information on 10 creatures
• All data is separated by a pound sign.
5 Structures (5 points)
• Cost Structure
• Creatures structure
10 Main Function (10 points)
• Creation of Creatures array (100 elements)
• Shows main menu / correct choices
• Runs until user chooses option 5
• Validate user choice
• Function call statements
• saveCreaturesToFile if user wants to after choosing option 5
15 enterCreatures Function (15 points)
• Check if current # of creatures is less than 100, error
message if equal to 100.
• Menu of two choices
• Validate user’s choice
CHOICE 1
• Ask user for filename
14. • Open file as input
• Check file for errors
• Read from file using getline, while loop (eof) and increment
num creatures.
• Close file
• Confirmation message
CHOICE 2
• Ask user for each piece of data in specified/neat way
• Read data from standard input
• Place data in Creatures array in correct element.
• Choice 2 happens as many times as user wants it to
• Confirmation message
Earned
Points
Possible Points
Detailed Description
10 deleteCreature Function (10 points)
• List each creature’s name
• Ask user which one they want to delete
• Send the name of the one to delete to mAE(), returning a
bool if it was removed.
• If removed, decrement num creatures
• Confirmation message if it was removed or not.
15. 5 moveArrayElements Function (5 points)
• Use for loop to see if string sent in matches any in the array.
• If found, for loop to move array elements & return true
• If not found, return false
15 printCreatures Function (15 points)
• Menu of two options
• Validate user’s choice
CHOICE 1
• Print each creature to the screen
• Neat/uses stream manipulators
• BONUS (5 points) does word wrapping for description
CHOICE 2
• Ask user for filename
• Open file as output file
• Print each creature to the file
• Neat/uses stream manipulators
• Close file
• Confirmation message
5 printStatistics Function (5 points)
• Cost of each creature per week is printed out with for loop
• Total cost of all creatures is printed out
5 saveCreaturesToFile Function (5 points)
• Filename is acquired from user
• File is opened as output file
16. • Use for loop to print out creatures to file with # between each
piece of data
• File is closed
• Confirmation message
10 Readability of
Code
Sufficient comments in code & naming of variables
Includes comment block at top containing title of program, date,
author, and purpose of program.
Code is indented properly.
(only
subtracts
from grade)
Delivery Program received by email before midnight -
Wednesday April 23rd.
DATE/TIME Delivered:
100 TOTAL (FINAL) GRADE