Array Function Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Nislipk
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Jan 10, 2010 8:03 pm

Array Function Help

Post by Nislipk »

Hi, I am attempting my first C++ game. I am writing a Text Based RPG and am havin trouble with a function.
Right now i have this. I plan on fixing the body up later but for now its what i need.

Code: Select all

void pickItem (string inv[5][5], int room, string item,int itemNum, string nothing)
{
	string option;
	if (room == itemNum)
	{
		cout << "There is a" << item << "in here. Do you want to pick it up? take or leave?";
		cin  >> option;
		if (option == "take")
		{
			if (inv[0][0] == nothing)
			{
				inv[0][0] = item;
				room = 0;
			}
			else if (inv[0][1] == nothing)
			{
				inv[0][1] = item;
				room = 0;
			}
			else if (inv[0][2] == nothing)
			{
				inv[0][2] = item;
				room = 0;
			}
			else if (inv[0][3] == nothing)
			{
				inv[0][3] = item;
				room = 0;
			}
			else if (inv[0][4] == nothing)
			{
				inv[0][4] = item;
				room = 0;
			}
			else if (inv[0][0] == nothing)
			{
				inv[0][0] = item;
				room = 0;
			}
			else if (inv[1][1] == nothing)
			{
				inv[1][1] = item;
				room = 0;
			}
			else if (inv[1][2] == nothing)
			{
				inv[1][2] = item;
				room = 0;
			}
			else if (inv[1][3] == nothing)
			{
				inv[1][3] = item;
				room = 0;
			}
			else if (inv[1][4] == nothing)
			{
				inv[1][4] = item;
				room = 0;
			}
			if (inv[2][0] == nothing)
			{
				inv[2][0] = item;
				room = 0;
			}
			else if (inv[2][1] == nothing)
			{
				inv[2][1] = item;
				room = 0;
			}
			else if (inv[2][2] == nothing)
			{
				inv[2][2] = item;
				room = 0;
			}
			else if (inv[2][3] == nothing)
			{
				inv[2][3] = item;
				room = 0;
			}
			else if (inv[2][4] == nothing)
			{
				inv[2][4] = item;
				room = 0;
			}
			else if (inv[3][0] == nothing)
			{
				inv[3][0] = item;
				room = 0;
			}
			else if (inv[3][1] == nothing)
			{
				inv[3][1] = item;
				room = 0;
			}
			else if (inv[3][2] == nothing)
			{
				inv[3][2] = item;
				room = 0;
			}
			else if (inv[4][3] == nothing)
			{
				inv[3][3] = item;
				room = 0;
			}
			else if (inv[3][4] == nothing)
			{
				inv[3][4] = item;
				room = 0;
			}
			if (inv[4][0] == nothing)
			{
				inv[4][0] = item;
				room = 0;
			}
			else if (inv[4][1] == nothing)
			{
				inv[4][1] = item;
				room = 0;
			}
			else if (inv[4][2] == nothing)
			{
				inv[4][2] = item;
				room = 0;
			}
			else if (inv[4][3] == nothing)
			{
				inv[4][3] = item;
				room = 0;
			}
			else if (inv[4][4] == nothing)
			{
				inv[4][4] = item;
				room = 0;
			}
		}
	}	
}
For some reason i get an error when I try to use it. The code is suppose to ask if you want to pick up an item and place it in an array called inventory. I keep getting the error "cannot convert 'std::string' to std:: string (*)[5] "

Code: Select all

if (player1.player_y == 2)
				{
					cout << room7.info<<endl;
					currentRoom.room	= 7;
					currentRoom.left	= room7.left;
					currentRoom.right	= room7.right;
					currentRoom.up		= room7.up;
					currentRoom.down	= room7.down;
					map [0][0] = 'X';
					map [1][0] = '-';
					map [2][0] = '-';
					map [0][1] = '-';
					map [1][1] = '-';
					map [2][1] = '-';
					map [0][2] = '-';
					map [1][2] = '-';
					map [2][2] = '-';
					pickItem (inventory[5][5], room7.item, health_potion.itemNum , health_potion.type, nothing.type );
					pickItem (inventory[5][5], room7.item, shield.itemNum , shield.type, nothing.type );	
					pickItem (inventory[5][5], room7.item, sword.itemNum , sword.type, nothing.type );	
					pickItem (inventory[5][5], room7.item, ring.itemNum , ring.type, nothing.type );	
				}
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Array Function Help

Post by GroundUpEngine »

Hi, Welcome to the Forum :)

Your problem is this I believe ->

Code: Select all

pickItem (inventory[5][5], room7.item, health_potion.itemNum , health_potion.type, nothing.type );
Instead use this -> because inventory is a string[5][5]

Code: Select all

pickItem (inventory, room7.item, health_potion.itemNum , health_potion.type, nothing.type );
Nislipk
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Jan 10, 2010 8:03 pm

Re: Array Function Help

Post by Nislipk »

lol actually that just gave me more errors.

/Users/Tommy/Desktop/RPG Test/main2.cpp:326: error: invalid conversion from 'int' to 'const char*'
/Users/Tommy/Desktop/RPG Test/main2.cpp:326: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
/Users/Tommy/Desktop/RPG Test/main2.cpp:326: error: cannot convert 'std::string' to 'int' for argument '4' to 'void pickItem(std::string (*)[5], int, std::string, int, std::string)'
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Array Function Help

Post by GroundUpEngine »

Damn :P
You have an Item class right? check that the member types match up to the pickItem arguments
Nislipk
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Jan 10, 2010 8:03 pm

Re: Array Function Help

Post by Nislipk »

its actually a structure and i had my two parameters backwards thanks...I works. Now my game went from being over 6000 lines of code to being about 1000-2000 lines. I appreciate the help. Oh and i started looking at ur development videos and the engine is pretty kool.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Array Function Help

Post by GroundUpEngine »

No Prob, I'm glad it works! and Thanks :D
Post Reply