|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I'm having a bit of trouble with this. I'm creating a cute little text adventure game for practice, so I can learn the basics of coding in C++.
I decided to use a vector for player inventory. It works great. It's a vector of objects, and I can put in or take out objects, or create a list of them for the "inventory" command, and it all works great. But I tried to do the same thing for my rooms, so they could have their own "inventories", and it didn't work. The only way I can think of that these two vectors are different is that one is contained in an object and one isn't. Specifically, I tossed this line into my Global Variables section: Code:
vector<Object> player_inventory; When I run my command to list the player's inventory, I call this function: Code:
int Inventory_List()
{
cout << "\nYou are carrying:\n";
if (player_inventory.size() == 0) {cout << "\nNothing\n";}; //checks whether there are no objects in the player's inventory
if (player_inventory.size() > 0)
{
int n;
n = player_inventory.size();
do
{
n--; // this is necessary because the elements in the vector are numbered starting with 0
cout << endl << player_inventory[n].title;
} while (n != 0);
cout << endl << endl;
};
}
So, since "cout << player_inventory[n].title" successfully outputted the title of an object in the player_inventory vector, I tried to use identical code to output the title of objects in the room's inventory: Code:
cout << Room_001.room_contents[n].title So the only difference I can think of is that my player_inventory vector is a global vector, but my room_contents vector is inside a "Room" object. Should that really make a difference? I'm sorry if my code snippits are a bit disjointed, but I think you'll get the jist. |
| Sponsored Links |
|
|
|
|||||
|
Quote:
By the way, I noticed you're using the operator[] for your vector; you should rather use vector::at. operator[] does not check whether the given index is out of range or not; vector::at does. If the given index is out of range, vector::at will throw a std:: out_of_range-exception. Code:
myVector.at(index);
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| create a deck of cards with objects | damien | Java Help | 6 | 01-11-2008 02:24 PM |
| Java:Tutorial - Making multiple objects work differently | John | Java Tutorials | 0 | 01-11-2007 02:10 PM |
| Java:Tutorial - Adding more objects to your window | John | Java Tutorials | 0 | 01-11-2007 02:09 PM |
| Creating an analog clock with ActionScript | AfTriX | Tutorials | 2 | 01-07-2007 02:19 AM |
| Objects | Nightracer | General Programming | 8 | 07-28-2006 08:02 PM |
| Xav | ........ | 1359.44 |
| MeTh0Dz|Reb0rn | ........ | 1072.63 |
| WingedPanther | ........ | 911.18 |
| marwex89 | ........ | 906.86 |
| morefood2001 | ........ | 899.18 |
| John | ........ | 887.37 |
| Brandon W | ........ | 770.65 |
| chili5 | ........ | 312.39 |
| Steve.L | ........ | 264.99 |
| dcs | ........ | 232.34 |
Goal: 100,000 Posts
Complete: 83%