[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  SMAUG coding
. . -> [Subject]  Reading and Printing Struc Contents

Reading and Printing Struc Contents

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Rash   United Kingdom  (56 posts)  [Biography] bio
Date Tue 05 Jan 2010 07:55 PM (UTC)

Amended on Tue 05 Jan 2010 09:20 PM (UTC) by Nick Gammon

Message
I'm having problems trying to list the contents of a struc to the player. I currently have this:


if ( !str_cmp( arg1, "list" ) )
{
     for ( i = 0; i < MAX_SMITH_ITEMS; i++ )
     {
	  pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table->name, i );
     }
     return;
}


Fist few lines of the struc;


const   struct  smith_items_type smith_items_table [] =
{
	{   0, "NULL",          0, 0, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "NULL"      }, /* 0 */
	{   1, "knife",          10, 10, 10,  5,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 0, 1, 4, 0, 0, 0, 0, 0, "pierce"      }, /* 1 */
	{   2, "cock-spur",      5,  5,  10,  4,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 0, 1, 3, 0, 0, 0, 0, 0, "pierce"  }, /* 2 */
	{   3, "stiletto",       12, 10, 15,  5,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 0, 1, 5, 0, 0, 0, 0, 0, "stab"	      }, /* 3 */
	{   4, "dagger",         15, 10, 20,  30,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 12, 1, 6, 2, 0, 0, 0, 0, "pierce"	      }, /* 4 */


And this is the output I get when I do 'forge list'


Item: NULL, Increment: 0
Item: NULL, Increment: 1
Item: NULL, Increment: 2
Item: NULL, Increment: 3
Item: NULL, Increment: 4
Item: NULL, Increment: 5
Item: NULL, Increment: 6
Item: NULL, Increment: 7
Item: NULL, Increment: 8
Item: NULL, Increment: 9
Item: NULL, Increment: 10
Item: NULL, Increment: 11
Item: NULL, Increment: 12
Item: NULL, Increment: 13
Item: NULL, Increment: 14
Item: NULL, Increment: 15


I think I see the problem but not sure how to solve it. It's not actually checking the each row of the struc. Its only checking the first line. I did original have;


pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table[i].name, i );


That just crashes the mud upon 'forge list'.

Where am I going wrong? Am I simply trying to access the data incorrectly? Any help would be very much appreciated.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Tue 05 Jan 2010 09:24 PM (UTC)
Message
Can you show the smith_items_type definition please?

I think your original code was closer, you have to index into the table, or everything will be the first item. I can't see offhand why that crashed.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Rash   United Kingdom  (56 posts)  [Biography] bio
Date Reply #2 on Tue 05 Jan 2010 09:51 PM (UTC)
Message
Here they are Nick;


extern const struct smith_items_type smith_items_table[];

/* structs setup */
struct smith_items_type
{
    int	  index;
	char *name;
	int   quantity;
	int   weight;
	int   difficulty;
	int   beats;
	int   wear_flags;
	int   item_type;
	int   base_v0;
	int   base_v1;
	int   base_v2;
	int   base_v3;
	int   base_pierce;
	int   base_bash;
	int   base_slash;
	int   base_exotic;
	char *dam_noun;
};
[Go to top] top

Posted by Wolf   USA  (1 post)  [Biography] bio
Date Reply #3 on Wed 06 Jan 2010 02:26 AM (UTC)

Amended on Wed 06 Jan 2010 02:56 AM (UTC) by Nick Gammon

Message
The original method makes more sense (iterating over the structs in the array of structs):


pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table[i].name, i );


Your "almost good" output shows that the array exists, you're dereferencing it with -> (same thing as smith_items_table[0]) and accessing the name element ("NULL"). Since i isn't involved, you get only "NULL" printed out.

I assume that MAX_SMITH_ITEMS is 16, because your loop tries to print out 16 items. Make sure your array of structs has 16 elements (with valid strings in the name position).

--
Wolf
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Wed 06 Jan 2010 03:05 AM (UTC)
Message
Good point about the number of items. Your code is a bit dangerous because the table has an undefined length but you are going through it MAX_SMITH_ITEMS times. How do you know the table is the correct size?

Instead of MAX_SMITH_ITEMS you could use NUMITEMS:


#define NUMITEMS(x) (sizeof(x) / sizeof(x[0]))


In this case, it would be:


for ( i = 0; i < NUMITEMS (smith_items_table); i++ )
 // blah blah


Now it iterates through for exactly the number of entries you put into it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Rash   United Kingdom  (56 posts)  [Biography] bio
Date Reply #5 on Wed 06 Jan 2010 10:29 PM (UTC)

Amended on Wed 06 Jan 2010 10:35 PM (UTC) by Rash

Message
I've defined its max size previous, I actually forgot to show that piece of code, as shown below:


#define MAX_SMITH_ITEMS  100


As you can see its defined as 100 max items (it does go through all 100 items, the output was cut from the example above to save on room).

I double checked the size of the struc as I had a naggling doubt and as yo both pointed it it may have smething to do with its size, low and behold it was missing 6 entries. I corrected that and nothing changed. So I've tried Nicks suggestion of:


for ( i = 0; i < NUMITEMS (smith_items_table); i++ )


When my code to use the above like so:


/* Not yet working, will list alll available items. */

	if ( !str_cmp( arg1, "list" ) )
	{
		for ( i = 0; i < NUMITEMS (smith_items_table); i++ )
		{
		  pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table .name, i );
		}
		return;
	}


It now works perfectly!


Item: NULL, Increment: 0
Item: knife, Increment: 1
Item: cock-spur, Increment: 2
Item: stiletto, Increment: 3
Item: dagger, Increment: 4
Item: keris, Increment: 5
Item: dirk, Increment: 6
Item: poinard, Increment: 7
Item: long-knife, Increment: 8
Item: shortsword, Increment: 9
Item: langsax, Increment: 10
Item: leaf-sword, Increment: 11
Item: gladius, Increment: 12
Item: cutlass, Increment: 13
Item: sabre, Increment: 14
Item: rapier, Increment: 15
Item: cinqueda, Increment: 16
Item: spatha, Increment: 17
Item: falchion, Increment: 18
Item: scimitar, Increment: 19
Item: nimcha, Increment: 20
Item: hanger, Increment: 21
Item: kastane, Increment: 22


Thank you very much guys. Nick that little bit of code worked great. Will have to use that in the future if I have problems outputting strucs like that.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,684 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]