Question: CONCEPTS: Branches & Loops String & Character Functions Programmer-Defined Functions Input & Output Files FILES TO TURN IN: You are given the three text files
CONCEPTS:
-
Branches & Loops
-
String & Character Functions
-
Programmer-Defined Functions
-
Input & Output Files
FILES TO TURN IN:
You are given the three text files but they should be included in your submission. You must zip all the following files in a zip file and then upload the zip file to ilearn. -5 points for not including all the files and submitting them in a zipped file!
-
program3.cpp
-
dinoDirectory.txt (provided for you)
-
carnivores.txt (provided for you)
-
herbivores.txt (provided for you)
DESCRIPTION / SPECIFICATIONS:
You are given a text file that has a data dump of dinosaur and other animal data. The format of the data in the file is as follows in this order:
-
Animal Name
-
Length and/or Width of animal
-
Mass/Weight of animal
-
What the animal eats
-
Description and facts about the animal
Each piece of information is separated by a #.
Your job is to read all the information from the file, determine if the animal is a dinosaur herbivore or carnivore. If it is a herbivore, you will print out the animals information in a neat, easy to read format in a output text file named herbOutFile.txt. If the animal is a dinosaur carnivore, you will print out the animals information in a neat, easy to read format in an output file named carnOutFile.txt. If the animal is not a dinosaur, then you will print it out in a text file named otherOutFile.txt. Also, you will keep count of how many animals from the file are:
-
Total carnivore dinosaurs
-
Total herbivore dinosaurs
-
Total dinosaurs
-
Total dinosaurs who are over 10,000 pounds
-
Total dinosaurs whose names contain the substring saurus
The output of this program is fairly short compared to the code required. You can see the sample output at the bottom of this document. Your output should match mine exactly, unless you change the dinosaurData.txt file.
REQUIRED FUNCTIONS
You may not have any global variables even if they are constant. This is so you must practice sending data to/from functions!
Here are the function prototypes that you will need for your programmer-defined functions below. You may NOT alter these function prototypes.
int carnOrHerb(string);
bool searchCarnivore(string);
bool searchHerbivore(string);
int printDino(ofstream&, ifstream&, string, int);
bool overTenGrand(string);
void printResults(int, int, int, int, int);
Main Function
-
Read in the filename of the data file from the user. (this is where the user will type dinoDirectory.txt as long as you dont rename the file)
-
Open the file. Validate the file can be opened and if not, allow the user to enter in a different filename until the file they enter in can be opened. [Validate with while loop!]
-
Start the loop to read from the file. I recommend your loop header be the following line while(getline(dinoDirFile, tempString, #)) which will read the animals name from the file. [Note: putting the # delimiter in while using getline will tell getline to read until it sees a # instead of just reading until it reaches a , which is the default delimiter.]
-
Inside this loop, you will do several things:
-
Does the name contain saurus? If so, add one to the variable keeping up with how many dinosaurs have the substring saurus.
-
Call the carnOrHerb function, sending the animal name. This function will return an integer. If it returns a 1, then the animal is a carnivore. Otherwise, if the carnOrHerb function returns a 2, then the animal is a herbivore. Otherwise, the animal is Other.
-
Depending on what the animal is, open the appropriate output file (carnOutFile.txt if it is a carnivore, herbOutFile.txt if it is an herbivore, or otherOutFile.txt if it is other). [Note: file must be opened in APPEND mode in order to work properly]
-
Then call the printDino function, sending the correct output file, the input file, the name of the animal, and the integer holding how many animals have weight over 10,000. This function will be responsible for reading in the rest of the data about the animal from the input file and printing it to the correct output file.
-
Dont forget to close the correct output file.
-
-
After the loop to read in all the data and print it to the output files, then you can close the input file.
-
Last, call the printResults function, sending all the integer total variables.
Make sure to check out the Sample Output Text Files given to you all in ilearn, which shows you what your carnOutFile.txt, herbOutFile.txt, and otherOutFile.txt should look like if your program works correctly.
carnOrHerb Function
Parameters: a string, holding the animals name Returns: an integer, indicating if the animal is a carnivore (1), herbivore (2), or other (-1)
This function should call the searchCarnivore function, sending the animals name. This function will return a Boolean value, indicating if the animal is a carnivore (true) or not (false). If the searchCarnivore function returned true, you should return a 1 from this function.
If not a carnivore, the searchHerbivore function should be called, sending the animals name. This function will return a Boolean value indicating if the animal is a herbivore (true) or not (false). If the searchHerbivore function returned true, you should return a 2 from this function.
If not an herbivore or carnivore, this function should return a -1.
searchCarnivore Function
Parameters: a string, holding the animals name
Returns: a Boolean, indicating true if the animal is a carnivore and false if it is not
This function should open the text file named carnivores.txt, which has a list of carnivore dinosaurs separated by a # delimiter. Once opened, you should read each dinosaur name with a while loop and check if the dinosaur name from the file matches the animal name sent to this function. If it does, then the animal is a carnivore and so you should return true.
If after checking every name from the file there are no matches, then this function should return false.
searchHerbivore Function
Parameters: a string, holding the animals name
Returns: a Boolean, indicating true if the animal is a herbivore and false if it is not
This function should open the text file named herbivores.txt, which has a list of carnivore dinosaurs separated by a # delimiter. Once opened, you should read each dinosaur name with a while loop and check if the dinosaur name from the file matches the animal name sent to this function. If it does, then the animal is an herbivore and so you should return true.
If after checking every name from the file there are no matches, then this function should return false.
printDino Function
Parameters: an output file stream of the output file you are printing to, an input file stream passed by reference of the input file (dinoDirectory.txt), a string containing the name of the animal, and the integer holding the number of dinosaurs that weigh over 10,000 lbs
Returns: an integer, holding the (possibly updated) number of dinosaurs that weigh over 10,000 lbs
-
Print the name to the output file.
-
Read the height/length from the input file.
-
Print the height/length to the output file.
-
Read the mass from the input file.
-
Call the overTenGrand function, which will return true if the dinosaur is over 10,000 lbs and if so, then increment the integer that keeps track of this.
-
Print the mass to the output file.
-
Read what the animal eats from the input file
-
Print what the animal eats to the output file.
-
Read the description of the animal from the input file.
-
Print the description to the output file.
-
Return the integer holding how many animals are over 10,000 lbs.
overTenGrand Function
Parameters: a string, holding the mass of the animal
Returns: a Boolean, true if the animal is over 10,000 lbs and false otherwise
This function is going to challenge you in the problem-solving area (which is why this class is introduction to computer programming AND problem solving)! You must use a combination of string class functions and possibly character functions in order to figure out if the animal could be over 10,000 lbs. The string sent to this function could be in one of the following examples:
-
30 to 60 lbs
-
5,000 to 9,000 lbs
-
9,000 to 10,000 lbs
-
11,000 to 15,000 lbs
The first two examples above should return false and the last two examples should return true. Think about how you can extract out of this string just what you need (without any commas) in order to test the number. You will have to convert the number to an actual integer data type to do a proper comparison.
Some of these pages on cplusplus.com may help with this task:
-
http://www.cplusplus.com/reference/string/string/
-
http://www.cplusplus.com/reference/string/
-
https://www.cplusplus.com/reference/cstdlib/
printResults Function
Parameters: five integers holding the totals that must be printed out for the results
Returns: none
This function will print out a horizontal row of dashes. One easy way to do this is to define a string like this:
string line(50, -);
Then you just have to cout << line in order to print out a row of 50 dashes.
Then, the function should print out the totals similar to the sample output. Make it very easy to read and lined up nicely.
Last, print out another horizontal row of dashes.
CODE READABILITY:
-
Provide a comment block at the top of your source file that contains the source file name, your name, creation date, and purpose of the program
-
Place a comment above each function telling the function name & purpose.
-
Name your variables where they describe what they hold
-
Indent your code properly and consistently.
SAMPLE OUTPUT:
Dinosaur Directory File Name (dinoDirectory.txt): dinoDirectory.txt
Tyrannosaurus Rex is being printed to the CARNIVORE file!
Ankylosaurus is being printed to the HERBIVORE file!
Stegosaurus is being printed to the HERBIVORE file!
Velociraptor is being printed to the CARNIVORE file!
Spinosaurus is being printed to the CARNIVORE file!
Triceratops is being printed to the HERBIVORE file!
Gerbil is being printed to the OTHER file!
Giganotosaurus is being printed to the CARNIVORE file!
--------------------------------------------------
TOTAL CARNIVORE DINOS : 4
TOTAL HERBIVORE DINOS : 3
TOTAL DINOS : 7
DINOS OVER 10,000 LBS : 5
DINO NAMES END IN 'SAURUS': 5
wat u mean by networking
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
