Thread: vector

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Exclamation vector

    This is going to be difficult to explain so I will ask you after this example code...

    Code:
    vector<string> inventory(10, "empty slot\n");
    
    //pseudocode
    if item 10 of inventory is "empty slot" and user buys an item
            then place an item in the vector over the first available "empty slot"
    
    else
          display to user that they have made an error
    well, I have got all of that to work except the if item 10 of inventory is "empty slot" . I have made a program that shows that you have 10 empty slots and when you go to the store and buy an item, the first available empty slot gets taken up by the purchased item. The only problem I seem to have is I can't get the program to check whether or not item 10 is an empty slot, and if not, does not try to put an item in the already full inventory.

    thnx

  2. #2
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    When using vectors there is no such thing as an empty slot. Vectors are sizable arrays. When you put something into the vector class allocates the needed memory for it and throws it into the vector container. Just as when you delete something, the vector class deallocates the memory, meaning it is not there any more. So all you need to do is have a int and increment it every time you add an item.

    Ex:
    Code:
    int counter;
    if(counter <= vectorName.size())
    {
       //add item to class
    }
    else
    {
     //message to the player: Sorry there are open slots
    }
    hope that helps

  3. #3
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    Quote Originally Posted by loopshot
    When using vectors there is no such thing as an empty slot
    I really sorry, I should have been more specific when I said "empty slot". I put 10 strings in the vector that were all "empty slot". I am other strings in place of the empty slots. I'm using empty slots to help display the inventory to the user, looks rather nice in my opinion.

    thanks for trying to help though

  4. #4
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    ok then here is the deal. using a vector to hold a string of a certian number of slots is bad design unless you are going to need lots and lots of arrays of that certian number. even in this case it is still bad practice because a vector is supposed to be thought of as a safe array. putting an array into an abstraction for arrays is completly defeating the purpose of a vector because it is making it unsafe, which is what the vector is supposed to counter. if you do the implementation i talked about you should be fine.

    hope that helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM