COSC 143 7 Programming Assignment 2 Programming Assignment 2 You will write a pr
ID: 3750083 • Letter: C
Question
COSC 143 7 Programming
Assignment 2 Programming Assignment 2
You will write a program that reads a binary file that contains records of creatures. Each record can be stored in a Creature struct. You should present the user with a menu of three options: display a specific creature, display all the creatures sorted by name, or quit. If the user chooses to display a specific creature, you should ask the user which creature to display.
You will be supplied with a starter file PA 2 _ Starter .cpp. You will need to complete some of the code in main a nd write the definitions for the three functions. For sorting, you should use the sort function found in the algorithm header file. You can read more about this function here:
http://www.cplusplus .com/articles/NhA0RXSz/ .
Make sure you use good programming style.
Name your file PA 2 _lastName_firstName.cpp, replacing lastName with your actual last name and firstName with your actual first name. Submit only the cpp file.
A possible sample run will look as follows:
Menu
1. Display a specific creature
2. Display all creatures sorted by name
3. Quit
Enter your choice (1, 2, 3): 1
Which creature do you want to view?
Enter 1 to 10: 3
Creature Report
Name: Kobold
Type: Small Humanoid
HP: 4
Armor Class: 15
S peed: 6
Press any key to continue . . .
Menu
1. Display a specific creature
2. Display all creatures sorted by name
3. Quit
Enter your choice (1, 2, 3): 1
Which creature do you want to view?
Enter 1 to 10: 8
Creature Report
Name: Frost Worm
Type: Huge Magical Beast HP: 147
Armor Class: 18
Speed: 6
Press any key to continue . . .
Menu
1. Display a specific creature
2. Display all creatures sorted by name
3. Quit
Enter your choice (1, 2, 3): 1
Which creature do you want to view?
Enter 1 to 10: 15
Inva lid entry. Try again.
Which creature do you want to view?
Enter 1 to 10: 2
Creature Report
Name: Orc Type: Medium Humanoid
HP: 5
Armor Class: 13
Speed: 6
Press any key to continue . . .
Menu
1. Display a specific creature
2. Display all creatures sorted by name
3. Quit
Enter your choice (1, 2, 3): 2
Basili s k
Medium Magical Beast
45
16
4
Clay Golem
Large Construct
90
22
4
Ethereal Filcher
Medium Abberation
22
17
8
Frost Worm
Huge Magical Beast
147
18
6
Goblin
Small Humanoid
5
15
6
Harpy
Medium Monstrous Humanoid
31
13
4
Hobgoblin
Medium Humanoid
6
15
6
Kobold
Small Humanoid
4
15
6
Medusa
Medium Monstrous Humanoid
33
15
6
Orc
Medium Humanoid
5
13
6
Press any key to continue . . .
Menu
1. Display a specific creature
2. Display all creatures sorted by name
3. Quit
Enter your choice (1, 2, 3): 3
Press any key to continue . . .
Explanation / Answer
Note: Just have to write some more code .I will complete this.Thank u
_______________
creaturesInfo.txt
Kobold
Small Humanoid
4
15
6
Harpy
Medium Monstrous Humanoid
31
13
4
Medusa
Medium Monstrous Humanoid
33
15
6
Frost Worm
Huge Magical Beast
147
18
6
Goblin
Small Humanoid
5
15
6
Orc
Medium Humanoid
5
13
6
Basili s k
Medium Magical Beast
45
16
4
Hobgoblin
Medium Humanoid
6
15
6
Clay Golem
Large Construct
90
22
4
Ethereal Filcher
Medium Abberation
22
17
8
_____________
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct Creature
{
string name;
string type;
int hp;
int armor;
int speed;
};
int main() {
int size=0;
string name;
string type;
string hp, armor,speed;
//defines an input stream for the data file
ifstream dataIn;
//Opening the input file
dataIn.open("creaturesInfo.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
while(getline(dataIn,name))
{
getline(dataIn,type);
getline(dataIn, hp);
// hp = atoi(price.c_str());
getline(dataIn, armor);
// armor = atoi(price.c_str());
getline(dataIn, speed);
// speed = atoi(price.c_str());
size++;
cout<<size<<endl;
}
cout<<size<<endl;
dataIn.close();
struct Creature c[size];
//Opening the input file
dataIn.open("creaturesInfo.txt");
for(int i=0;i<size;i++)
{
getline(dataIn,name);
getline(dataIn,type);
getline(dataIn,hp);
getline(dataIn,armor);
getline(dataIn,speed);
c[i].name=name;
c[i].type=type;
c[i].hp=atoi(hp.c_str());
c[i].armor=atoi(armor.c_str());
c[i].speed=atoi(speed.c_str());
}
dataIn.close();
for(int i=0;i<size;i++)
{
cout<<c[i].name<<endl;
cout<<c[i].type<<endl;
cout<<c[i].hp<<endl;
}
}
return 0;
}
__________________