A problem with vectors

This is a discussion on A problem with vectors within the C++ Programming forums, part of the General Programming Boards category; Im not sure if I should be posting this here or the game programming forum. Anyway...the deal is... I am ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    A problem with vectors

    Im not sure if I should be posting this here or the game programming forum.

    Anyway...the deal is... I am using vectors instead of arrays to track PLAYER potions.

    here is the code...I commented where the problems lie...there is over 400 lines of code so I am only going to post snippets.


    Top of Town.cpp where I declared the vector and its iterator.
    Code:
    #include "Library.h"
    #include "Globals.h"
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    // I am not too sure of where to declare this vector
    std::vector<POTION>potions(15);
    std::vector<POTION>::iterator i;
    
    void Armory(PLAYER*);
    void ItemShop(PLAYER*);
    ItemShop function
    Code:
    void ItemShop(PLAYER* player)
    {
         cout << "As you step inside, a pleasant smell wafts up to your nose.  You see\n"
              << "a kindly heavyset older woman watering plants, that decorate the shop.\n" << endl;
         Pause();
         cout << "What would you like to do?\n"
              << "<1> Talk to the woman.\n"
              << "<2> Exit\n"
              << ">";
         int answer = -1;
         cin  >> answer;
         cin.clear();
         cin.ignore(INT_MAX,'\n');
         
         if (answer == 1)
         {
             cout << "Hello Traveler!  Welcome to Suzies Pots and Herbs.\n"
                  << "How may I help you?\n" << endl;
             Pause();
             Clr_Scrn();
             
             bool shoploop = true;
    
             while (shoploop = true)
             {
             cout << "What do you want to do?\n"
                  << "<1> Buy Potions\n"
                  << "<2> Buy Spells\n"
                  << "<3> View Stats\n"
                  << "<4> Exit\n"
                  << ">";
             int choice = -1;
             cin  >> choice;
             cin.clear();
             cin.ignore(INT_MAX,'\n');
             
             switch(choice)
             {
                  case 1:
                       {
                           POTION* potionlist = new POTION[7];
                           potionlist[0].CreatePotion("Small Health Potion", 10, 30, 50);
                           potionlist[1].CreatePotion("Medium Health Potion", 30, 100, 100);
                           potionlist[2].CreatePotion("Large Health Potion", 100, 300, 1000);
                           potionlist[3].CreatePotion("Elixer", 9999, 9999, 10000);
                           potionlist[4].CreatePotion("Small Mana Potion", 10, 50, 100);
                           potionlist[5].CreatePotion("Medium Mana Potion", 30, 100, 100);
                           potionlist[6].CreatePotion("Large Mana Potion", 100, 300, 1000);
                           
                           cout << "Name: " << "Restore: " << "Price: " << endl;
                           for (int loop = 0; loop < 6; loop++)
                           {
                               cout << loop + 1 << potionlist[loop].GetName() << " " << 
                               potionlist[loop].GetMinRestore() << "-" << potionlist[loop].GetMaxRestore()
                               << potionlist[loop].GetPrice() << endl;
                               }
                           cout << "0: Exit\n"
                                << "What do you want to buy?\n";
                           int decision = -1;
                           cin  >> decision;
                           cin.clear();
                           cin.ignore(INT_MAX,'\n');
                           
                           if (decision > 0 && decision < 8)
                           {
                                 if (player->GetGold() >= potionlist[decision-1].GetPrice())
                                 {
                                      player->SpendGold(potionlist[decision-1].GetPrice());
                                      
                                      // places potion in vector
                                      potions.push_back(potionlist[decision-1]);
                                      
                                      // this is only to test to see the capacity of the vector
                                      // for some reason it outputs 30--when should be 15
                                      cout << endl << potions.capacity() << endl;
                                      
                                      // to test how much memory is inialized inside the vector
                                      // which for some reason outputs 16--when should be 1
                                      cout << potions.size() << endl << endl;
                                      
                                      // put this in just to see what the heck was going on and am now
                                      // more confused than ever!!!!
                                      // first 15 outputs is dobldegook
                                      // the 16th is the potion that you actually bought!!!
                                      for(i = potions.begin(); i != potions.end(); ++i)
                                      {
                                            cout << i->GetName() << endl;
                                            }
                                      
                                      cout << "You have purchased a " << potionlist[decision-1].GetName()
                                      << endl;
                                      cout << "Do you want to make another purchase? 1 for yes 2 for no\n";
                                      cin  >> shoploop;
                                      cin.clear();
                                      cin.ignore(INT_MAX,'\n');
                                      Clr_Scrn();
                                      delete [] potionlist;
                                      break;
                                      }
                                 else
                                 {
                                      cout << "You cant afford this!!\n" << endl;
                                      Pause();
                                      Clr_Scrn();
                                      delete [] potionlist;
                                      shoploop = true;
                                      break;
                                      }
                                      }
                           else if (decision == 0)
                           {
                                delete [] potionlist;
                                shoploop = false;
                                break;
                                }
                           else
                           cout << "Invalid Selection" << endl;
                           shoploop = true;
                           Pause();
                           Clr_Scrn();
                           break;
                           }
    Last edited by eaane74; 04-18-2007 at 03:20 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > potionlist[6].CreatePotion("Large Mana Potion", 100, 300, 1000);
    Well this is off the end of your allocated memory, so fix that first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    ouch...I did that with all of my dynamic arrays

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Code:
    std::vector<POTION>potions(15);
    That code creates a vector with 15 elements (meaning size() is 15). The capacity can be anything 15 or higher. When you use push_back later, you are adding an element so the size is then 16. The capacity won't change after push_back unless it is too small to hold one more element.

    If you want to start with an empty vector and use push_back, create it with this:
    Code:
    std::vector<POTION>potions;
    Or, you can create it like you did originally and just not use push_back.

    BTW, the reason "dobldegook" is output for the first 15 is that they are default initialized. If your POTION class or struct does not have a constructor to initialize its members, then they may contain uninitialized values.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Okay....that makes sense

    The reason I did that way was because I dont want the vector to exceed 15 items.

    I guess I could do it this way then
    Code:
    if (potions.size() == 15)
    {
          cout << "Your inventory is already full!" << endl;
    }
    or something like that

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >// I am not too sure of where to declare this vector
    >std::vector<POTION>potions(15);
    push_back creates a new element, so you don't need to create 15 elements here, unless you use something like:
    Code:
    potions[count] = potionlist[decision-1];
    to store the new potion. Instead you can just use:
    Code:
    std::vector<POTION> potions;
    As far as where to declare it, if you made a class you could declare it in the class. Otherwise in main(), and you'd pass this vector to any function which needed it.
    Last edited by swoopy; 04-18-2007 at 03:47 PM.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    yeah I thought about declaring it in the class, but wasnt sure about how to do it...
    passing the vector to a functions sounds much easier.

    this is my first time working with vectors...

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    // I am not too sure of where to declare this vector
    std::vector<POTION>potions(15);
    Declare the vector where it makes sense. Perhaps you could create a WORLD* structure or something and pass it to your functions along with the PLAYER*.

    Code:
    cin.ignore(INT_MAX,'\n');
    Perhaps this would be a better idea:
    Code:
    cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    I can't remember if it's streamsize, streambuf, or one of those with an underscore in it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Perhaps this would be a better idea:
    Code:

    cin.ignore(std::numeric_limits<std::streamsize>::m ax(),'\n');

    I can't remember if it's streamsize, streambuf, or one of those with an underscore in it.
    That works..

    What is the difference between the two?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, the std::streamsize code is better because that's the actual data type that is used. INT_MAX is usually what std::numeric_limits<std::streamsize>::max() is set to, but not always. See here for a better description: Istream cin vs. cin.get

    Oh yes, and apparently (I had forgotten this) streamsize is in <ios>:
    Code:
    #include <ios>      // For streamsize
    why do some programs work on some compliers while others dont?

    But even using INT_MAX is great. Most people just use 9999 or something.
    Last edited by dwks; 04-18-2007 at 04:37 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Code:
    while (shoploop = true)
    Typo?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    no...its my error ridden shoploop thingamajig ....I have been meaning to change it for a while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with vectors and classes
    By applenerd in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 06:36 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Vector Problem
    By Morgul in forum Game Programming
    Replies: 16
    Last Post: 02-25-2006, 02:27 PM
  4. Problem with Vectors And Pointers
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 01-21-2006, 06:23 PM
  5. Shell Sort with Vectors Problem
    By TheSpoiledMilk in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2005, 02:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21