- Code: Select all
// Main.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>
#include <conio.h>
#include <sstream>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::stringstream;
bool intToStr( string &str, int n );
void handleErrorThings();
class cItem
{
protected:
int duribility;
string itemdescription;
string itemname;
int weight;
string type;
public:
cItem(string lol, string desc, int ww, int duri, string tt)
{
itemname = lol;
duribility = duri;
itemdescription = desc;
weight = ww;
type = tt;
}
virtual const string sGetDesc()
{
return itemdescription;
}
const string sGetName()
{
return itemname;
}
const int iGetWeight()
{
return weight;
}
const int iGetDuribility()
{
return duribility;
}
const string sGetType()
{
return type;
}
};
class cWeapon : public cItem
{
private:
int damage;
public:
cWeapon(string nn, int dd, int weight, int duri, string desc, string type) : cItem(nn, desc, weight, duri, type)
{
damage = dd;
}
const int iGetDamage()
{
return damage;
}
virtual const string sGetDesc()
{
string conver = "";
intToStr(conver, damage);
string newdesc = itemdescription + "\n Damage: " + conver;
return newdesc;
}
};
class cArmor : public cItem
{
private:
int armor;
public:
cArmor(string nn, int aa, int ww, int duri, string desc, string tt) : cItem(nn, desc, ww, duri, tt)
{
armor = aa;
}
const int iGetArmor()
{
return armor;
}
virtual const string sGetDesc()
{
string conver = "";
intToStr(conver, armor);
string newdesc = itemdescription + "\n Armor: " + conver;
return newdesc;
}
};
int main(int argc, char *argv[])
{
cin.exceptions(cin.failbit);
try
{
vector<cItem*> itemSYSTEM;
while(true)
{
system("cls");
cout << "Item MENU" << endl << endl;
cout << "[1] Make a new item" << endl;
cout << "[2] Delete an item" << endl;
cout << "[3] View all items" << endl;
cout << "[4] Quit" << endl;
char x;
do x = _getch(); while(x != '1' && x != '2' && x != '3' && x != '4');
switch(x)
{
case '1':
{
system("CLS");
cout << "Choose a type of item" << endl;
cout << "________________________" << endl;
cout << endl;
cout << " [1]Armor" << endl;
cout << " [2]Weapon" << endl;
cout << " [3]Other" << endl;
char lol;
do lol = _getch(); while(lol != '1' && lol != '2' && lol != '3' && lol != '4');
switch(lol)
{
case '1':
{
system("CLS");
cout << "Armor Creation Menu" << endl;
string name = "";
cout << "Please enter the name of the item:" << endl;
getline(cin, name);
int Armor = 0;
cout << "Please enter the amount of armor:" << endl;
cin >> Armor;
int Duribility = 0;
cout << "Please enter the duribility of the armor:" << endl;
cin >> Duribility;
int Weight = 0;
cout << "Please enter the weight of the armor:" << endl;
cin >> Weight;
cin.sync();
string Itemdesc = " ";
cout << "Please enter the item description:" << endl;
getline(cin, Itemdesc);
cout << endl;
cout << endl;
cout << "Thank you for creating the item." << endl;
itemSYSTEM.push_back( new cArmor(name, Armor, Weight, Duribility, Itemdesc, "Armor"));
system("PAUSE");
}
break;
case '2':
{
system("CLS");
cout << "Weapon Creation Menu" << endl;
string name = "";
cout << "Please enter the name of the item:" << endl;
getline(cin, name);
int Damage = 0;
cout << "Please enter the amount of damage:" << endl;
cin >> Damage;
int Duribility = 0;
cout << "Please enter the duribility of the weapon:" << endl;
cin >> Duribility;
int Weight = 0;
cout << "Please enter the weight of the weapon:" << endl;
cin >> Weight;
cin.sync();
string Itemdesc = " ";
cout << "Please enter the item description:" << endl;
getline(cin, Itemdesc);
cout << endl;
cout << endl;
cout << "Thank you for creating the item." << endl;
itemSYSTEM.push_back( new cWeapon(name, Damage, Weight, Duribility, Itemdesc, "Weapon"));
system("PAUSE");
}
break;
case '3':
{
system("CLS");
cout << "Other Item Creation Menu" << endl;
string name = "";
cout << "Please enter the name of the item:" << endl;
getline(cin, name);
int Duribility = 0;
cout << "Please enter the duribility of the item:" << endl;
cin >> Duribility;
int Weight = 0;
cout << "Please enter the weight of the item:" << endl;
cin >> Weight;
cin.sync();
string Itemdesc = " ";
cout << "Please enter the item description:" << endl;
getline(cin, Itemdesc);
cout << endl;
cout << endl;
cout << "Thank you for creating the item." << endl;
itemSYSTEM.push_back( new cItem(name, Itemdesc, Weight, Duribility, "Other"));
system("PAUSE");
}
break;
default:
cout << "ERROR" << endl;
}
}
break;
case '2':
{
system ("CLS");
cout << "Delete Menu" << endl << endl;
if(itemSYSTEM.size() == 0){
cout << "There are no items to be displayed." << endl;
cin.sync();
cin.get();
}
else
{
cout << " Below you will find a list of all of the items." << endl;
cout << "Please choose one of them to delete." << endl;
cout << "__________________________________________________" << endl << endl;
int i = 0;
for(; i < itemSYSTEM.size(); i++)
{
cout << "_______________________" << endl;
cout << i+1 << "." << endl;
cout << " Name: " << itemSYSTEM[i]->sGetName() << endl;
cout << " Type: " << itemSYSTEM[i]->sGetType() << endl;
cout << " Duribility: " << itemSYSTEM[i]->iGetDuribility() << endl;
cout << " Description: " << itemSYSTEM[i]->sGetDesc() << endl;
cout << " Weight: " << itemSYSTEM[i]->iGetWeight() << endl;
cout << "_______________________" << endl;
}
cout << endl << endl;
int itemz;
cout << "Please choose which item you would like to delete." << endl;
cin >> itemz;
if(itemz < 1) break;
else if(itemz > itemSYSTEM.size()) break;
else
{
delete itemSYSTEM[itemz -1];
itemSYSTEM.erase( itemSYSTEM.begin() + (itemz - 1));
}
}
}
break;
case '3':
{
system("CLS");
cout << " Below you will find a list of all of the items." << endl;
cout << "__________________________________________________" << endl << endl;
int i = 0;
for(; i < itemSYSTEM.size(); i++)
{
cout << "_______________________" << endl;
cout << i+1 << "." << endl;
cout << " Name: " << itemSYSTEM[i]->sGetName() << endl;
cout << " Type: " << itemSYSTEM[i]->sGetType() << endl;
cout << " Duribility: " << itemSYSTEM[i]->iGetDuribility() << endl;
cout << " Description: " << itemSYSTEM[i]->sGetDesc() << endl;
cout << " Weight: " << itemSYSTEM[i]->iGetWeight() << endl;
cout << "_______________________" << endl;
}
cout << endl << endl;
cout << "Please press any key to return to the main menu." << endl;
_getch();
}
break;
case '4':
{
return 0;
}
}
}
return 0;
}catch(...)
{
handleErrorThings();
}
}
bool intToStr( string &str, int n )
{
stringstream ss;
//this copies str into ss.
ss << n;
//This copies data in ss into n, converting it to the correct type at the same time.
//If the process succeeds the function will return true. If it fails the function will
//return false.
return ( ss >> str );
}
void handleErrorThings()
{
cerr << "Um ERROR ON UR INPUT" << endl;
char BadInput[50];
cin.sync();
cin.clear();
cout << "Press any key to return to the main menu." << endl;
_getch();
}
I am C++ trainer dog testing your skizzilz.

