C++ Text-Based RPG Inventory System - c++

I'm currently taking a programming 2 class (c++), we've been tasked to make a text based rpg. I'm using this post as a reference for my inventory system, as I think it's pretty effective. But I keep running into a E0349 "no operator "==" or "<<" matches these opperands" error.
If anyone could help me that would be great. Here is my full set of code:
#include "pch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <ostream>
#include <Windows.h>
#include <string>
#include <cctype>
using namespace std;
struct Item {
string name; //Item name.
int slot; //Head, Torso, Hands
int attack;
int knowledge;
int defense;
int hp;
int speed;
int charisma;
};
int main()
{
//Variables, Strings, etc.
int itemcounter = 0, counter = 0;
//"Empty" Item
Item Empty{ "<Empty>", 0, 0, 0 };
vector<Item> Equipment = { 6, Empty }; //Current Equipment, 6 empty slots.
vector<Item> Inventory = { }; //Player Inventory.
string InventorySlots[] = { "Head" "Torso", "Hands" }; //Player parts where items can be equiped.
cout << "You sit your bag down and take a look inside." << " You have:" << endl;
for (int i = 0; i < itemcounter; i++)
{
cout << InventorySlots[i];
if (Equipment[i] == "Empty ")
{
cout << " " << Equipment[i] << endl << endl;
}
}
}
Here is where my errors are more specifically
for (int i = 0; i < itemcounter; i++) //Display equipped
{
cout << InventorySlots[i];
if (Equipment[i] == "Empty ") //Error Here
{
cout << " " << Equipment[i] << endl << endl; //Errore Here
}
}
Error Message
Error (active) E0349 no operator "<<" matches these operands C:\Users\USER\source\repos\clunkinv\clunkinv\clunkinv.cpp 47

Equipment[i] is an object of type Item. If you don't provide a method to compare your object with "Empty" the compiler can't know how to compare in line
if (Equipment[i] == "Empty ")
Either you compare the property
if (Equipment[i].name == "Empty ")
or you have to provide a method. Same problem in line
cout << " " << Equipment[i] << endl << endl;
The compiler can't know how to print your object. You have to provide a function for this.
You could
struct Item {
string name; //Item name.
int slot; //Head, Torso, Hands
int attack;
int knowledge;
int defense;
int hp;
int speed;
int charisma;
};
std::ostream &operator<<(std::ostream &os, const Item& item) {
os << item.name;
return os;
}
You have to overload operators for your classes if you want to use them.

Related

Why the function doesn't recognize an object created in main()?

I have to make a Shopping Cart program for school, and there are more moving parts in this program than I've ever had to deal with. I'm trying to figure out why the function in my main.cpp can't recognize the object that I created in main(). It keeps saying that
the cart is not declared in the scope.
I still have to finish building out the menu, but I can't even get it to recognize the object that gets created in main.
I can see that the object is being properly created, because it can be manipulated within the main() no problem. Furthermore, I even have a few placeholder commands in there to get it working. The thing is that the homework assignment requires the menu to be in a function.
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
using namespace std;
void PrintMenu()
{
while (true)
{
string choice;
cout << "MENU" << endl;
cout << "a - Add item to cart" << endl;
cout << "d - Remove item from cart" << endl;
cout << "c - Change item quantity" << endl;
cout << "i - Output item's descriptions" << endl;
cout << "o - Output shopping cart" << endl;
cout << "q - Quit" << endl;
cout << endl;
cout << "Choose an option: " << endl;
cin >> choice;
if (choice == "a") {
cart.GetDate();
}
else if (choice == "d") {
}
else if (choice == "c") {
}
else if (choice == "i") {
}
else if (choice == "o") {
}
else if (choice == "q") {
break;
}
else {
cout << "That is not a valid choice" << endl;
}
}
}
int main()
{
string name;
string date;
cout << "Enter customer's name: " << endl;
cin >> name;
cout << "Enter today's date: " << endl;
cin >> date;
cout << endl;
cout << "Customer name: " << name << endl;
cout << "Today's date: " << date << endl;
ShoppingCart cart(name, date);
ItemToPurchase apple("apple", 1, 4, "apple");
cout << cart.GetDate();
cart.AddItem(apple);
cout << cart.GetNumItemsInCart();
PrintMenu();
}
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include <string>
#include <iostream>
#include <vector>
#include "ItemToPurchase.h"
using namespace std;
class ShoppingCart
{
private:
string customerName = "none";
string currentDate = "January 1, 2016";
vector<ItemToPurchase> cartItems;
public:
ShoppingCart(string name, string date);
string GetCustomerName() const;
string GetDate();
void AddItem(ItemToPurchase);
void RemoveItem(string);
void ModifyItem();
int GetNumItemsInCart();
double GetCostofCart();
void PrintTotal();
string PrintDecriptions();
};
#endif
#include <iostream>
#include <string>
#include <vector>
#include "ShoppingCart.h"
using namespace std;
ShoppingCart::ShoppingCart (string name, string date){
customerName=name;
currentDate= date;
}
string ShoppingCart::GetCustomerName() const
{
return customerName;
}
string ShoppingCart::GetDate()
{
return currentDate;
}
void ShoppingCart::AddItem(ItemToPurchase item)
{
cartItems.push_back(item);
}
void ShoppingCart::RemoveItem(string name)
{
for (int i = 0; i < cartItems.size(); i++)
{
if (cartItems.at(i).GetName() == name)
{
cartItems.erase(cartItems.begin() + i);
}
else
{
cout << "Item not found in cart. Nothing removed." << endl;
}
}
}
int ShoppingCart::GetNumItemsInCart(){
int number;
number = cartItems.size();
return number;
}
double ShoppingCart::GetCostofCart()
{
double sum = 0.0;
for (int i = 0; i < cartItems.size(); i++)
{
sum += cartItems[i].GetQuantity() * cartItems[i].GetPrice();
}
return sum;
}
#include "ItemToPurchase.h"
void ItemToPurchase::SetName(string SetItemName){
itemName = SetItemName;
}
void ItemToPurchase::SetPrice(int SetItemPrice){
itemPrice = SetItemPrice;
}
void ItemToPurchase::SetQuantity(int SetItemQuantity){
itemQuantity = SetItemQuantity;
}
string ItemToPurchase::GetName() const {
return itemName;
}
int ItemToPurchase::GetPrice() const {
return itemPrice;
}
int ItemToPurchase::GetQuantity() const {
return itemQuantity;
}
#include <string>
#include <iostream>
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
using namespace std;
class ItemToPurchase
{
public:
ItemToPurchase(string a, int b, int c, string d)
{itemName = a;
itemPrice = b;
itemQuantity = c;
itemDescription =d;
}
void SetName(string SetItemName);
void SetPrice(int SetItemPrice);
void PrintItemDescription();
void SetQuantity(int SetItemQuantity);
string GetName() const;
int GetPrice() const;
int GetQuantity() const;
void SetDescription() const;
string GetDiscription() const;
void PrintItemCost() const;
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
};
#endif
[...], but I can't even get it, to recognize the object that gets created in main().
The main() and the PrintMenu() are two different functions with different scope. One can not know the variables from other, unless you pass or make known by any means.
In your case, you can pass the ShoppingCart object (i.e cart) from main() to the PrintMenu function, so that inside it will know which cart you meant for:
void PrintMenu(ShoppingCart& cart)
// ^^^^^^^^^^^^^^^^^^^^
{
// ...
}
and call the function from main() with the ShoppingCart object.
PrintMenu(cart);
That being said;
Please do not practice with using namespace std;. Read more: Why is "using namespace std;" considered bad practice?
If the member function does not modify the member, you should mark the function as const. Applies for all the getters of both ItemToPurchase and ShoppingCart classes.
When you return a non-trivial copyable objects like std::string in a getter, you should be avoiding copying. That means you might want to have changes like as below for all your getters which returns std::string:
const std::string& GetDate() const /* noexcept */
{
return currentDate;
}
This line in PrintMenu is the problem:
cart.GetDate();
The compiler looks for something called cart within the scope of that function, which does not exist. Once way to resolve this, is to pass a reference to the cart created in main to the PrintMenu function:
void PrintMenu(ShoppingCart &cart){
and call the function like this:
PrintMenu(cart);
Note that because, in the future, you'll want to modify cart in the menu, you'll need to pass it as a reference (i.e. with &) and not as a copy or constant reference.

object is undefined in function

Im new to C++ and started working with classes recently for a school excercice.
I really cant see whats wrong and after creating an object "player" to the Hero class i can't use that object later in the "main Menu" function to call a method because i get the "identifier is undefined" error!
Any suggestions?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Hero
{
private:
//member variables
string playername;
public:
//constructor
Hero(string name)
{
playername = name;
}
string getName()
{
return playername;
}
};
//start 1
void mainMenu()
{
cout << " - - - |" << player.getName() << "- - - \n";
}
void setPlayer()
{
string name;
cout << "Hello, what is your name? " << endl;
getline(cin, name);
Hero player(name);
mainMenu();
}
int main()
{
int selection;
cout << "Shadow of darkness\n ";
cout << "1.) Start ";
cout << "2.) Exit ";
cin >> selection;
if (selection == 1)
setPlayer();
else if (selection == 2)
exit (0);
else
main();
return 0;
}
OK, calling main() from main() is a forbidden (as explained here), so do not do it.
Here is a typical example with your class (the class is cool as you have it, I just added an initializer list for fun):
#include <iostream>
#include <string>
using namespace std;
class Hero
{
private:
//member variables
string playername;
public:
//constructor
Hero(string name) : playername(name)
{
}
string getName()
{
return playername;
}
};
int main()
{
Hero player("Daniel");
cout << "Player's name: " << player.getName() << std::endl;
return 0;
}
Output:
Player's name: Daniel
Based on this, try to work your logic and do all sort of stuff that you long for (after reading some books/tutorials)!

Best practice for using input data from external file in different functions

For a small project in c++ I need to initialize a number of variables from a text file and make these available for several functions in the code. The way I do it at the moment is to create a class that reads and returns those values and make a single global instance of the class that is then accessible by all functions in the project. However, to me this seems not a very elegant solution and I am wondering what would be the best practice here. Parsing the variables as arguments is not really an option for me as I typically need many these variables in the different functions.
Thanks for any suggestions!
MWE:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <sstream>
using namespace std;
class indata {
int age;
double weight;
double length;
public:
indata ();
int getAge(void)
{
return age;
}
double getWeight(void)
{
return weight;
}
double getLength(void)
{
return length;
}
};
indata::indata () {
ifstream inputfile;
inputfile.open ("input.dat");
inputfile>>age;
inputfile>>weight;
inputfile>>length;
inputfile.close();
};
// make class instance global
indata in;
double Length2inch()
{
const double cm2inch = 1./2.54;
double Length = in.getLength();
return Length*cm2inch;
};
int main () {
cout << "Age : " << in.getAge() << endl;
cout << "Weight : " << in.getWeight() << endl;
cout << "Length : " << in.getLength() << endl;
cout << endl;
cout << "Length in inches: " << Length2inch() << endl;
return 0;
}
input.dat:
32
75
175

How to initialize instances of classes?

//main.cpp
#include "Monsters.h"
#include "Hero.h"
#include "Monsters.h"
#include "Characters.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
When i created my object it took those parameters and initialised them to it right?
So why when i called their methods, it returned a different value?
For example, I initialized them here while created my object at the same time:
Hero Me(100,20,30,40);
Monsters m(100,16,18,20);//creates a monster object and uses overloaded constructor to initialize
Me.getHp();//expected 100, but got a long negative random number
m.getHp();//expected 100, but got a long negative random number also
//Why must i use setHp() and setAttack() when i already initialized them with the constructor?
Me.setAttack(89);
Me.setHp(100);
m.setAttack(40);
m.setHp(100);
cout << "\nAttacking!\n";
while ((Me.getHp() > 0) && (m.getHp() > 0))
{
cout << "\nYour hp is: " << Me.getHp() << endl;
cout << "The enemy's hp is: "<< m.getHp() << endl;
cout << "\nThe monster has attacked you!\n";
cout << "You received " << m.getAttack() << " damage;" << endl;
Me.damageTaken(m.getAttack());
if(Me.getHp() > 0)//Check if still alive
{
cout << "\nYour hp is now: " << Me.getHp() << endl;
//cout << "Enemy hp is: "<< m.getHp() << endl;
cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() << " Damage" << endl;
m.damageTaken(Me.getAttack());
if(m.getHp() > 0)//Check if still alive
{
cout << "Enemy hp is now: " << m.getHp() << endl;
cout << "\nAttacking again!\n";
}
}
}
if ((Me.getHp() > 0) && (m.getHp() <= 0))
cout <<"\nCongratulations! You killed the enemy!" << endl;
else if ((Me.getHp() <= 0) && (m.getHp() > 0))
cout << "You have died!" << endl;
cin.sync();
cin.get();
return 0;
}
Here's the rest of the code in case you need it:
//Hero.h
#pragma once
#include "Characters.h"
class Hero:
public Characters
{
private:
int Herolevel;
int HeroHp;
int HeroStrength;
int HeroAttack;
int HeroDefense;
public:
//Hero();
Hero(int, int, int, int);
~Hero();
};
//Hero.cpp
#include "Monsters.h"
#include "Hero.h"
#include "Characters.h"
#include <iostream>
using namespace std;
//Hero::Hero()
//{
// cout << "HOLA! Hero Created using normal constructor\n";
//}
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
: HeroHp(newHp), Herolevel(newLevel), HeroAttack(newAttack), HeroDefense(newDef)
{
cout << "Hero created using Overloaded function!\n";
HeroHp = newHp;
cout << "Hp is: "<< HeroHp << endl;
Herolevel = newLevel;
cout << "level is: " << Herolevel << endl;
HeroAttack = newAttack;
cout << "Attack is: " << HeroAttack << endl;
HeroDefense = newDef;
cout << "Defense is: " << HeroDefense << endl;
}
Hero::~Hero()
{
cout << "Hero destroyed!\n";
}
//Monsters.h
#pragma once
#include "Characters.h"
class Monsters:
public Characters //Hero
{
private:
int Monsterlevel;
int MonsterHp;
int MonsterStrength;
int MonsterAttack;
int MonsterDefense;
public:
//Monsters();
Monsters(int, int, int, int);
//Monsters(int);
~Monsters();
};
//Monsters.cpp
#include "Monsters.h"
#include "Hero.h"
#include "Characters.h"
#include <iostream>
using namespace std;
Monsters::Monsters(int newHp, int newLevel, int newAttack, int newDef)
: MonsterHp(newHp), Monsterlevel(newLevel), MonsterAttack(newAttack), MonsterDefense(newDef)
{
cout << "Monster created using Overloaded function!\n";
MonsterHp = newHp;
cout << "Hp is: "<< MonsterHp << endl;
Monsterlevel = newLevel;
cout << "level is: " << Monsterlevel << endl;
MonsterAttack = newAttack;
cout << "Attack is: " << MonsterAttack << endl;
MonsterDefense = newDef;
cout << "Defense is: " << MonsterDefense << endl;
}
Monsters::~Monsters()
{
cout << "\nMonster Destroyed";
}
//Characters.h
#pragma once
class Characters
{
private:
int level;
int Hp;
int Strength;
int Attack;
int Defense;
public:
Characters();
Characters(int);
Characters(int, int, int, int);
~Characters();
int getAttack();
int getDefense();
int getStrength();
int getHp();
int getLevel();
void setAttack(int);
void setDefense(int);
void setStrength(int);
void setHp(int);
void setlevel(int);
void damageTaken(int);
};
//Characters.cpp
#include "Characters.h"
#include "Hero.h"
#include "Monsters.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
Characters::Characters()
{
cout << "\nCharacter has been created!\n";
}
Characters::Characters(int random)
{
cout << "Level " << level << " character created with: \n";
srand ((unsigned)time(0));
random = rand() % 10 + 1;
//setlevel(int random);
level = random;
}
Characters::~Characters()
{
cout << "Character has been destroyed!\n";
}
void Characters::setAttack(int att)//get Character left over hp
{
Attack = att;
}
void Characters::setDefense(int def)//get Character left over hp
{
Defense = def;
}
void Characters::setStrength(int str)//get Character left over hp
{
Strength = str;
}
void Characters::setHp(int health)//get Character left over hp
{
Hp = health;
}
void Characters::damageTaken(int damage)//get Character left over hp
{
Hp -= damage;
}
void Characters::setlevel(int lvl)//get Character left over hp
{
level = lvl;
}
int Characters::getAttack()
{
//cout << "Your attack is: " << Attack << endl;
return Attack;
}
int Characters::getDefense()
{
//cout << "Your defense is: " << Defense << endl;
return Defense;
}
int Characters::getStrength()
{
//cout << "Your strength is: " << Strength << endl;
return Strength;
}
int Characters::getHp()
{
//cout << "Your hp is: " << Hp << endl;
return Hp;
}
int Characters::getLevel()
{
//cout << "Your level is: " << level << endl;
return level;
}
How to initialize instances of classes?
You need to correctly initialize your base classes by calling their constructor, passing on the relevant arguments. Maybe it is easier to illustrate this with a simpler example:
class Foo
{
public:
explicit Foo(int i) : foo_i(i) {}
int getFoo() const { return foo_i; }
private:
};
class Bar : public Foo
{
public:
explicit Bar(int i) : Foo(i) // calls Foo(int) constructor, setting Foo::foo_i
{}
};
#include <iostream>
int main()
{
Bar b(42);
b.getFoo(); // prints 42
}
Hero inherits Characters, including all its data members (level, hp, etc.).
So, it doesn't need to add its own HeroLevel, HeroHP etc. You're just duplicating data (and work).
Now, it also doesn't initialize the base class members - the default constructor for Characters doesn't even zero them, so their values are undefined.
You should be looking for something like this:
class Characters
{
private:
int level;
int hp;
int strength;
int attack;
int defense;
protected:
Characters(); // zero everything by default
Characters(int); // randomly generate everything
Characters(int, int, int, int); // populate explicitly
public:
int getAttack() const { return Attack; }
// etc.
};
Characters::Characters() : level(0), hp(0), strength(0), attack(0), defense(0) {}
Characters::Characters(int seed) {
// NB. your code still doesn't initialize hp, strength etc.
// it also logs level before initializing it, so that will be garbage
}
Characters::Characters(int hit, int lvl, int att, int def)
: level(lvl), hp(hit), attack(att), defense(def)
{
// you only pass 4 attributes, what should strength be?
}
and finally:
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
: Characters(newHp, newLevel, newAttack, newDef)
{
// logging goes here
// note that you don't need HeroLevel etc. at all any more, just use level
}
You should really try to understand the basics of this before designing an entire class hierarchy - see juanchopanza's answer for a clear example. A smaller example would also have been much easier to paste, read and understand.

C++ Operator '<<' error

I have a question regarding a homework assignment.
I have two classes. One is called ticket.cpp, and the other is called TicketOrder.cpp
The main is within the ticket.cpp.
I am using a g++ compiler on Linux.
What I'm doing is trying to is print out a vector of a TicketOrder object called orders, but it gives me the following error:
ticket.cpp:57: error: no match for 'operator<<' in 'std::cout << orders. std::vector<_Tp, _Alloc>::operator[] with _Tp = TicketOrder, _Alloc = std::allocator'
Here is my code:
ticket.cpp
#include <iostream>
#include <vector>
#include <limits>
#include <cctype>
#include "TicketOrder.cpp"
using namespace std;
int main ()
{
int numberoftickets=0;
string input2;
char input3;
int profit=0;
vector <TicketOrder> orders;
int atotalmoney=0;
int btotalmoney=0;
int ctotalmoney=0;
int dtotalmoney=0;
int etotalmoney=0;
do
{
cout << "\nPick a ticket that you would like to buy: \n\n";
cout << "(A) Students without an activity card: $2.00 \n";
cout << "(B) Faculty and staff: $3.00 \n";
cout << "(C) USC alumni: $5.00 \n";
cout << "(D) UCLA students and alumni: $20.00 \n";
cout << "(E) Everyone else: $10.00 \n";
cin >> input3;
if (input3=='A')
{
cout << "How many tickets do you wish to buy? " <<endl;
if (numberoftickets >0)
{
TicketOrder order;
order.setQuantity(numberoftickets);
order.setType(input3);
orders.push_back(order);
for (int i=0; i< orders.size(); i++)
{
cout << orders[i];
}
}
}
else
{
cout << "Sorry did not recognize input, try again. " << endl;
}
} while (input3 != 'S');
TicketOrder.cpp:
#include <iostream>
using namespace std;
class TicketOrder
{
public :
//Getters
int getQuantity() const
{
return quantity;
}
char getType() const
{
return type;
}
//Setters
void setQuantity (int x)
{
quantity=x;
}
void setType(char y)
{
type =y;
}
private:
char type;
char quantity;
};
As the compiler is clumsily trying to explain, the code is missing an operator<< for the TicketOrder class.
class TicketOrder {
public:
friend std::ostream& operator<<(std::ostream& os, TicketOrder const& order) {
os << "Type: " << type << ", quantity: " << quantity;
return os;
}
char type;
int quantity;
};
(Note: you probably want to change quantity to int.)
You must add the operator << function as a friend to be able to print values from your TicketOrder objects with cout. Further reading
You're attempting to use the << operator on cout and a TicketOrder object. That is undefined. You should use the TicketOrder object to generate a string first, then output that via cout. Either that, or you can define the << operator for the TicketOrder class, as described in one of the other two answers.