This is for a C++ intro class lab:
Here are the instructions:
Create a Spell Catalog for a RPG.
It should greet the user, & inform them that you can type "exit" to exit the program, or type "all" to print all of the entries in the catalog.
They will enter the name of the Spell.
The program will search the catalog to see if the spell is already in the catalog.
If it is, it will print out the spells' name, magic school, description and damage amount.
If it isn't, it will ask for the spell school, description, and damage amount.
The print "all" feature will print out the spell name, school, description, and damage amount.
You must use an enum for the magic schools (Fire, Water, Nature, Air, and Rock).
You must use a struct for each spell (name, school, description, damage)
You must create another struct for the catalog which will store a vector of spells
You must verify all inputs are valid -- that is, when you ask the user for certain data (like enums or ints),
that they give you the correct input. If not, you should warn them and re-ask for the input.
Also you must handle all-caps, no-caps, and first letter capitalized words.
You cannot force your user to use uppercase or lowercase, but you may convert their input.
You must use a "print" function that prints out the name of the spell, the school, the description and damage.
You must use the same function to print out a single spell and all of the spells.
The function does not need to know which situation it is printing for!
--------------------------------------------------------------------------------
MY CODE:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
enum magic_schools
{
FIRE,
WATER,
NATURE,
AIR,
ROCK
};
//------------------------------------------------------
// STRUCTS - spells (name, school, description, damage)
//------------------------------------------------------
struct Spell
{
string name;
magic_schools school;
string description;
int damage;
};
//------------------------------------------------------
// STRUCTS - catalog: only holds vector of spells
//------------------------------------------
struct Catalog
{
vector<Spell> spells;
};
// -----------------initalization---------------------------
Spell spell1;
spell1.name = "";
spell1.school = magic_schools;
spell1.description = "";
spell1.damage = "";
spells.push_back(spell1); // first word???!
// ---------------------------------------------------------
// FUNCT'S
void printSpell()
{
cout << spell1.name << spell1.school << spell1.description << spell1.damage << endl;
}
int main()
{
cout << endl;
cout << "Welcome to the super-cool Spell Catalog Program! :)" << endl;
cout << "====================================================" << "\n\n";
cout << "Type exit to exit, & all to print all..." << endl;
cout << "Or type in a spell name below:" << "\n\n";
string input;
getline(cin, input);
if( input == "all" || input == "ALL" || input == "All" || input == "a" )
{
printSpell(); // do print funct
}
else if ( input == "exit" || input == "EXIT" || input == "Exit" || input == "e" )
{
// EXIT
}
else
{
cin >> spell1.name;
}
cout << "What magic school does it belong to: FIRE, WATER, NATURE, AIR, or ROCK?" << endl;
cout << "Enter F, W, A or R:" << endl;
getline( cin, input );
//magic_schools magic_school;
if( input == "f" || input == "F" )
{
cin << magic_schools = FIRE;
}
else if( input == "w" || input == "W" )
{
cin << magic_schools = WATER;
}
else if( input == "n" || input == "N" )
{
cin << magic_schools = WATER;
}
else if( input == "a" || input == "A" )
{
cin << magic_schools = AIR;
}
else if( input == "r" || input == "R" )
{
cin << magic_schools = ROCK;
}
else
{
cout << "Invalid input, please try again." << endl;
cout << ">";
}
cout << "Enter the description." << endl;
cin >> spell1.description;
cout << "Enter how much damage the spell does." << endl;
cin >> spell1.damage;
printSpell();
system("pause");
}
--------------------------------------------------------------------------------
how it should turn out:
http://dl.dropbox.com/u/56862555/Lab_8.exe