Sign in to follow this  
Timmygyu

VECTOR HELL!!

Recommended Posts

F.S. Sorry for the screwy formatting, I copyed my code in from dev-c++ and it messed everything up. I think it's mostly fixed now. AHHHHHH!!! Different start than usual huh? Okay, so here's my problem: I'm making a text exploration game with some RPG elements. I am currently working on the items system. I am declaring the Item classes--Items and two working derived classes: Weapons and armour--and creating a menu to list and manipulate them. Neither the menu or the item classes are completly finished, but they are to the point where they should function. I wanted to test compile to see if I could at least list my items and equip-unequip them. So I wrote my code and debugged it to the point that it compiled. I thought it would work now. It didn't. I finaly guessed that it had something to do with my two vectored classes: weapons and armour. Since they are inherited classes I guess I need different syntax somewhere. The problem is I don't get to town much (It's a long story) and my one book doesn't even touch on vectors. I've tried google, but with no results, this is the kind of thing you just can't find! Anyway here's my code below. Bye, Tim "There's an exception to every rule, including this one"
/**************************Items,ItemList,SelectItem.**************************/


//This function can be called from any place in the program, henceforth a global
//function. It lists your current items. By typing in the keyword for the item
//you want to manipulate you can bring up a list of 4 differnt things you can do
//to the item, equip, unequip, use, and description. From here it can call
//differnt functions depending on what you want to do.

#include <iostream>
#include <string>
#include <vector>
/******************************************************************************/
/*                            Level One:Base Item Class                      */
/******************************************************************************/ 

//Item Class

int HP_Player = 1000;
int STR_Player = 100;
int DEF_Player = 100;
int EVAS_Player = 100; 
int ACCU_Player = 100;
int SPD_Player = 100;


 class Item
  {      
   public:
     virtual void Description() {std::cout << "\nThis is an item";}  
     int GetAmount() {return Amount;}
     void SetAmount(int TempAmount) {Amount=TempAmount;} 
     int GetWeight() {return Weight;}
     void SetWeight(int TempWeight) {Weight=TempWeight;} 
     string GetName() {return Name;}
     void SetName(int TempName) {Name=TempName;}      

   private:
     int Amount;
     int Weight;
     string Name;
  };
    


/******************************************************************************/
/*                              Level Two: Item Types                         */
/******************************************************************************/

//Weapon Class
//Note: The constructer takes the item input and inserts it into "Construct" variables.
//Note:The construct function takes the information in the "Construct" variables
//and inserts them into the "Temp" variables.
//
class Weapon : public Item
  {
  public:
    bool GetEquipedStatus() {return EquipedStatus;}
    void SetEquipedStatus(bool TempEquipedStatus) {EquipedStatus = TempEquipedStatus;}
    virtual void StatEdit(int, int, int, int);
    Weapon::Weapon() {Name=""; Weight=0; Temp_WEP_STR=0; Temp_WEP_EVAS=0; Temp_WEP_ACCU=0; Temp_WEP_SPD=0;}
    Weapon::Weapon(string Construct_WEP_Name="", int Construct_WEP_Weight=0,
                   int Construct_WEP_STR=0, int Construct_WEP_EVAS=0, 
                   int Construct_WEP_ACCU=0, int Construct_WEP_SPD=0)
    {
    Name = Construct_WEP_Name; 
    Weight = Construct_WEP_Weight;
    Temp_WEP_STR = Construct_WEP_STR; 
    Temp_WEP_EVAS = Construct_WEP_EVAS; 
    Temp_WEP_ACCU = Construct_WEP_ACCU; 
    Temp_WEP_SPD = Construct_WEP_SPD;
    }  
  private:
    bool EquipedStatus; 
    int Temp_WEP_STR; 
    int Temp_WEP_EVAS; 
    int Temp_WEP_ACCU; 
    int Temp_WEP_SPD;
  };
  
  void Weapon::StatEdit(int WEP_STR=0, int WEP_EVAS=0, int WEP_ACCU=0, int WEP_SPD=0)
  {
   WEP_STR = Temp_WEP_STR; 
   WEP_EVAS = Temp_WEP_EVAS; 
   WEP_ACCU = Temp_WEP_ACCU; 
   WEP_SPD = Temp_WEP_SPD;

   if (EquipedStatus = false)
   {
    EquipedStatus = true;
    STR_Player = STR_Player + WEP_STR;
    EVAS_Player = EVAS_Player + WEP_EVAS;
    ACCU_Player = ACCU_Player + WEP_ACCU;
    SPD_Player = SPD_Player + WEP_SPD;
   }

   else

   {
    EquipedStatus = false;
    STR_Player = STR_Player - WEP_STR;
    EVAS_Player = EVAS_Player - WEP_EVAS;
    ACCU_Player = ACCU_Player - WEP_ACCU;
    SPD_Player = SPD_Player - WEP_SPD;
   }
            
  };
vector<Weapon> WeaponVTR;    
////////////////////////////////////////////////////////////////////////////////

//Armour Class

class Armour : public Item
  {
  public:
    bool GetEquipedStatus() {return EquipedStatus;}
    void SetEquipedStatus(bool TempEquipedStatus) {EquipedStatus = TempEquipedStatus;}
    virtual void StatEdit(int, int, int, int);
    Armour::Armour() {Name=""; Weight=0; Temp_ARM_DEF=0; Temp_ARM_EVAS=0; Temp_ARM_ACCU=0; Temp_ARM_SPD=0;}
    Armour::Armour(string Construct_ARM_Name="", int Construct_ARM_Weight=0,
                   int Construct_ARM_DEF=0, int Construct_ARM_EVAS=0,
                   int Construct_ARM_ACCU=0, int Construct_ARM_SPD=0)
    {
    Name = Construct_ARM_Name; 
    Weight = Construct_ARM_Weight;
    Temp_ARM_DEF = Construct_ARM_DEF; 
    Temp_ARM_EVAS = Construct_ARM_EVAS; 
    Temp_ARM_ACCU = Construct_ARM_ACCU; 
    Temp_ARM_SPD = Construct_ARM_SPD;
    }  
  private:
    bool EquipedStatus; 
    int Temp_ARM_DEF; 
    int Temp_ARM_EVAS; 
    int Temp_ARM_ACCU; 
    int Temp_ARM_SPD;
  };
  
  void Armour::StatEdit(int ARM_DEF=0, int ARM_EVAS=0, int ARM_ACCU=0, int ARM_SPD=0)
  {
   ARM_DEF = Temp_ARM_DEF; 
   ARM_EVAS = Temp_ARM_EVAS; 
   ARM_ACCU = Temp_ARM_ACCU; 
   ARM_SPD = Temp_ARM_SPD;

   if (EquipedStatus = false)
   {
    EquipedStatus = true;
    DEF_Player = DEF_Player + ARM_DEF;
    EVAS_Player = EVAS_Player + ARM_EVAS;
    ACCU_Player = ACCU_Player + ARM_ACCU;
    SPD_Player = SPD_Player + ARM_SPD;
   }

   else

   {
    EquipedStatus = false;
    DEF_Player = STR_Player - ARM_DEF;
    EVAS_Player = EVAS_Player - ARM_EVAS;
    ACCU_Player = ACCU_Player - ARM_ACCU;
    SPD_Player = SPD_Player - ARM_SPD;
   }

  };  
vector<Armour> ArmourVTR;  
////////////////////////////////////////////////////////////////////////////////  

//Food/Medicne
/*
class Food/Medicne : public Item
  {
  public:
    bool GetUseInBattle() {return UseInBattle;}
    void SetUseInBattle(bool TempUseInBattle) {UseInBattle = TempUseInBattle;}
    bool GetPermanentEffect() {return PermanentEffect;}
    void SetPermanentEffect(bool TempPermanentEffect) {PermanentEffect = TempPermanentEffect;}
    void StatEdit();
  
  private:
    bool UseInBattle;
    bool PermanentEffect; 
    int Amount;
  };
  
  void Food/Medicne::StatEdit()
  {
    HP_Player = HP_Player + FM_HP;
    STR_Player = STR_Player + FM_STR;
    DEF_Player = DEF_Player + FM_DEF;
    EVAS_Player = EVAS_Player + FM_EVAS;
    ACCU_Player = ACCU_Player + FM_ACCU;
    SPD_Player = SPD_Player + FM_SPD;        
  }  
                                                                                                                                                                                                                                                                                                                                                                                      
*/                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
   

//////////////////////// //////////List Items///////////////////////////////////

int ListItems() 
{
 int PauseProgram;

 std::cout <<"
 Items: Name     Amount
 \n";
/* for(int i; i<FoodMediceneVTR.size(); i++)
 { 
  std::cout <<"   "FoodMediceneVTR[i].GetName()<<" :  " 
                   <<FoodMedicene[i].GetAmount()<<"\n";
 }
*/
 std::cin >> PauseProgram;
 
 std::cout <<"
 Weapons: Name     Weight
 \n";
 for(long j; j<WeaponVTR.size(); j++)
 { 
  std::cout <<"   "<<WeaponVTR[j].GetName()<<" :  "; 
  std::cout <<WeaponVTR[j].GetWeight()<<"\n";
 }

 std::cin >> PauseProgram;
 
 std::cout <<"
 Armour: Name     Weight
 \n";
 for(int k; k<ArmourVTR.size(); k++)
 { 
  std::cout <<"   "<<ArmourVTR[k].GetName()<<" :  " 
                   <<ArmourVTR[k].GetWeight()<<"\n";
 }

 std::cin >> PauseProgram;

}            

///////////////////////////////////Select Item//////////////////////////////////

void SelectItem(int Choice)
{
 int TestOne;
 int TestTwo;
 int TestThree;
 int TestFour;
 string TempItemName = "";
 for(;;)
 {
 if(Choice == 1) //If you want to equip an item...
 {
  std::cout <<"
  Equip which item? : ";
  std::cin >> TempItemName;

   if( TempItemName == WeaponVTR[TestOne].GetName() ) //If you entered a weapon name...
   {
    for(int l; l<WeaponVTR.size(); l++) //Looking for the item you requested...
    {
     if( WeaponVTR[l].GetAmount() < 1 ) //Do you have the item?
     {
      std::cout <<"
      You don't have that item
      \n\n";
      break;
     }

     else
     {
      WeaponVTR[l].SetEquipedStatus(true);     //Equip the item.
      break;
     }
    }
   }


  else

  if( TempItemName == ArmourVTR[TestTwo].GetName() ) //If you entered a armour name...
  {
   for(int m; m<ArmourVTR.size(); m++) //Looking for the item you requested...
   {
    if( ArmourVTR[m].GetAmount() < 1 )  //Do you have the item?
    {
     std::cout <<"
     You don't have that item
     \n\n";
     break;
    }

    else
    {
     ArmourVTR[m].SetEquipedStatus(true);   //Equip the item.
     break;
     }
    }
   }


  else  //If you screwed up...
  {
   std::cout <<"
   Not a item name, try again
   \n";
   continue;
  }
 } 
 
 if(Choice == 2)  //if you want to unequip an item...
 {
  std::cout <<"
  Equip which item? :
  \n";
  std::cin >> TempItemName;

  if( TempItemName == WeaponVTR[TestThree].GetName() ) //If you entered a weapon name...
  {
   for(int n; n<WeaponVTR.size(); n++) //Looking for the item you requested...
   {
    if( WeaponVTR[n].GetAmount() < 1 ) //Do you have the item?
    {
     std::cout <<"
     You don't have that item
     \n\n";
     break;
    }

    else
    {
     WeaponVTR[n].SetEquipedStatus(false);     //Unequip the item.
     break;
     }
    }
   }

  
  else

  if( TempItemName == ArmourVTR[TestFour].GetName() ) //If you entered a armour name...
  {
   for(int o; o<ArmourVTR.size(); o++) //Looking for the item you requested...
   {
    if( ArmourVTR[o].GetAmount() < 1 )  //Do you have the item?
    {
     std::cout <<"
     You don't have that item
     \n\n";
     break;
    }

    else
    {
     ArmourVTR[o].SetEquipedStatus(false);   //Unequip the item.
     break;
     }
    }
   }


  else  //If you screwed up...
  {
   std::cout <<"
   Not a item name, try again
   \n";
   continue;
  }
 }
 
  if(Choice == 3) //if you want to use an item...
 {
  std::cout <<"
  Equip which item? :
  \n";
  continue;
 }
  if(Choice == 4) //if you want the description of an item...
 {
  std::cout <<"
  Equip which item? :
  \n";
  continue;
 }  
 }
}

///////////////////////////////////////Items////////////////////////////////////

void Items() 
{
 int TempItemChoice;

 Weapon TempWeaponOne ("Small Axe", 5, 5, 7, 2, 9);
 WeaponVTR.push_back(TempWeaponOne);
 Weapon TempWeaponTwo ("Medium Axe", 10, 5, 7, 2, 9);
 WeaponVTR.push_back(TempWeaponTwo);
 Weapon TempWeaponThree ("Big Axe", 20, 5, 7, 2, 9);
 WeaponVTR.push_back(TempWeaponThree);
 Weapon TempWeaponFour ("Gigantic Axe", 50, 5, 7, 2, 9);
 WeaponVTR.push_back(TempWeaponFour);
 
 Armour TempArmourOne ("Gauntlet", 5, 5, 7, 2, 9);
 ArmourVTR.push_back(TempArmourOne);
 Armour TempArmourTwo ("HeadPeice", 10, 5, 7, 2, 9);
 ArmourVTR.push_back(TempArmourTwo);
 Armour TempArmourThree ("Chest Plate", 20, 5, 7, 2, 9);
 ArmourVTR.push_back(TempArmourThree);
 Armour TempArmourFour ("Full Suit", 50, 5, 7, 2, 9);
 ArmourVTR.push_back(TempArmourFour);

 ListItems();

 for(;;)
 {
 std::cout << "
 (1)Equip
 (2)Unequip
 (3)Use
 (4)Description
 (5)Exit
 \n";

 std::cin >> TempItemChoice;
 
 if(TempItemChoice == 5)
  {
  break;
  }

 else

 if( TempItemChoice == 0 || TempItemChoice > 5 ) 
  {
  std::cout << "Wrong number, try again\n";
  continue;
  }

 else
  {
   SelectItem(TempItemChoice);
   continue;
  }
 }
}       


int main()
{
Items();
return 0;
}


Share this post


Link to post
Share on other sites
Hi,

You sure? I just opened the same file in dev-c++, it compiled. What compiler are you using?

Bye,
Tim

"There is an exception to every rule, including this one"

Share this post


Link to post
Share on other sites

  • vector, string, and the global streams all are in the std namespace.
  • You can't split strings over multiple lines. Adjacent strings will be concatenated by the compiler though, so you can write: "foo\n" and "bar\n" on the next line, for code presentation purposes.
  • You have a bunch of for looks in which you are using an uninitialized loop variable. Similarly, your TestOne, TestTwo ... variables are uninitialized.
  • Your Item class should probably have a virtual destructor.
  • Its private members cannot be accessed by the derived classes. Try protected instead.
  • If you have a function with all-defaulted parameters, you can't also have a parameterless version. That applies to constructors too.
  • operator>> reads one word, not a line, try getline(cin, whatever);, otherwise it'll interpret "Small Axe" as "Small" (item not found) and then "Axe" (not found either).

Share this post


Link to post
Share on other sites
Hi,

Thanks a lot, more errors than I expected. I fixed it so it will run now. I can't belive I forgot namespace, or that dev-C++ let me compile without it! Oh well, thnks again

Bye,
Tim

"There's an exception to every rule, including this one"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this