Thread: Tables, and calling/referencing against them

  1. #1
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Smile Tables, and calling/referencing against them

    I'm currently making a program for a mud. It takes strings entered by the user and saves them in a file. Once that is finished it connects to the mud, and sends the strings out creating the character. I then plan on having an auto stat roller. However i've ran into a snag with this. Since each alignment of the mud gets a different set of races. I have to check it before I write it to the function. This is what I have so far. I'm wondering how I can create that into a table, and check against it. Also how to erase this ugly function and replace it with a more compact printf of the table itself. I'm a novice in code, and am self taught. I hope that explains why my code isn't exactly professional. :-P

    So basically take a look, and give me an example of how to use printf and fprintf in conjunction with tables, and how to set up the table. Any help would be great.


    This is the printf function I was speaking of.
    Code:
    // Prints the races for good alignment
    void do_good()
    {
    	printf("------------------------------------------\n\r");
    	printf("human         costs   0 experience points.\n\r");
    	printf("dwarf         costs 500 experience points.\n\r");
    	printf("elf           costs 500 experience points.\n\r");
    	printf("centaur       costs 400 experience points.\n\r");
    	printf("gnome         costs 400 experience points.\n\r");
    	printf("draconian     costs 300 experience points.\n\r");
    	printf("changeling    costs 300 experience points.\n\r");
    	printf("halfling      costs 300 experience points.\n\r");
    	printf("minotaur      costs 300 experience points.\n\r");
    	printf("arborian      costs 500 experience points.\n\r");
    	printf("------------------------------------------\n\r");
    }
    This is part of the main function i'm using to write to the file.

    Code:
    RACE:
    	printf("What is your race to be?\n\r");
    
    		if (alignment == 'G')
    		{
    			do_good();
    			gets(race);
    			// Now I need to check the race, which is a lot of || statements.
    			// If it returns falsely I use goto to go back to the start of
    			// the function.
    			fprintf(outfile,race); //I'm assuming if it passes the if statements i can print it
    		}
    		else if (alignment == 'N')
    		//then go through the same thing
    Last edited by Twiggy; 03-25-2003 at 02:39 PM.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>RACE:
    Ohh man, is this really a label? or you don't mean to use goto in your programs?

    >>gets(race);
    don't use gets, use instead fgets() and don't forget to check if he leaved '\n' after you got the string, read the manual for fgets().

    >>fprintf(outfile,race);
    well, this souldn't be something like
    Code:
    fprintf(outfile,"%s",race);
    fprintf(outfile,"%s\n\r",race);

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Vber
    >>RACE:
    Ohh man, is this really a label? or you don't mean to use goto in your programs?
    [/code]
    I believe it's trying to be a case statement.

    For the original poster, you don't use standard output streams for a mud. You need to do it all socket based. Are you writing this from scratch or working from an existing code base? Typically you want something like this:
    Code:
    struct race
    {
        char * name;
        unsigned long int experience_cost;
        ... stuff ...
    };
    
    ...
    
    struct race race_table[RACES] = 
    {
        { "elf", 500, ... },
        { "dwarf", 750, ... },
    };
    
    for( x = 0; x < RACES; x++ )
        output_somehow( "%s\t\tcosts %ul experience\n",
            race_table[x].name,
            race_table[x].experience_cost );
    You should probably pay a visit to The Mud Connector. A word of advice: When you get there, don't post newbie topics in the advanced board, it ........es everyone off.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    I'm using goto incase the string entered wasn't what i wanted, so i can return it to the beginning of the function.

    I've also got a way of writing this all to the socket. This is going to be a stand alone dos program (Since windows programs are a bit to advanced for my simple brain) that sends info from the text file once produced to the mud. Then when the stat rolling commences it will display the stat, and send the approriate command. I'm just trying to get the first part setup right now.

    Thanks for the help on the table. That should make it a bit easier, and less time consuming.
    To error is human, to really foul things up requires a computer

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>I'm using goto incase the string entered wasn't what i wanted,
    >> so i can return it to the beginning of the function.
    Ohh boy, program your program correctly, and you will not need using goto, you can make a function to get the correct input, and a do-while until you get it, without any goto and label's.

  6. #6
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Give me a demonstration? I learn better by example. Using goto is the only way I know how to return something to where I want it to be. Also what is the big no no associated with using goto?
    To error is human, to really foul things up requires a computer

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    goto's kill structured programming, think of them as the devil!!! There isn't anything out there that cant be done without the use of goto's. If you want to use goto's, i suggest learning "Basic".

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >There isn't anything out there that cant be done without the use of goto's.
    I'll be sure to inform the authors of Linux that the entire OS needs a rewrite using "proper" techniques. Oh yes, and expect a signifigant decrease in performance after they finish. My point being that you "can" drive a nail with a screwdriver, but a hammer is the more appropriate tool. If the best option is a goto then by all means use it.

    One thing that annoys me is people who put a complete ban on a tool simply because someone told them it was a bad thing. You should learn how to use the tool, how it works in general, and make up your own mind about whether other tools can better be used in its place. There are situations where an intelligently used goto can greatly enhance readability and performance.

    Note: In this case a goto is not the best option, but making an absolute statement such as "so and so is always wrong!" is a good way to get chewed out.

    -Prelude
    My best code is written with the delete key.

  9. #9
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    One thing that annoys me is people who put a complete ban on a tool simply because someone told them it was a bad thing.
    Sorry, did not mean to annoy you like that. I was simply stating my opinion.

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ok Prelude, so there is something that it's possible to do with gotos and impossible in another way? I don't think so... and no one here told me that goto is bad, I read in a document of an pro programmer and speaking about why we should avoid using gotos, well I'm trying to find this document again.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Vber
    Ok Prelude, so there is something that it's possible to do with gotos and impossible in another way? I don't think so...
    The point isn't that it's possible only using goto, but rather that rarely it may be more benificial to do so.

    Consider a hugely nested loop set. You could "break out of" them all by adding extra conditioinal checks to each loop break:
    Code:
    for( foo = bar; bar < fwee && exit_status != something; stuff++ )
        for( fie = foe; foe < fum && exit_status != something; add_naseum++ )
            for( this = that; the == other_thing && exit_status != something; morethings++ )
    Repeat that a few dozen more times, and you've just added a slew of checks each time every loop executes. A single goto statement would improve performance here. I think that's the point anyway.

    However, that being said, back to the original request:
    Code:
    switch( toupper( input[0] ) )
    {
        case 'A':
            if( strcmp( input, "Alpha" )) 
            {
    
            }
            else
            if( strcmp( input, "Apple" ) )
            {
    
            }
            else
            ....
        break;
        case 'B':
            if( strcmp( input, "Ball" )) 
            {
    
            }
            else
            if( strcmp( input, "Bravo" ) )
            {
    
            }
            else
            ....
        break;
        ....
    }
    There's one way to parse input. As I've already mentioned, The Mud Connector is a good resource for the original poster. You'll commonly see the DIKU variety typically implement a FSM (Finite State Machine) which uses a similar approach:
    Code:
    switch( descriptor->state )
    {
        case CON_CONNECTING:
            ...
        break;
        case CON_DISCONNECT:
            ...
        break;
        case CON_NEW_PLAYER:
            ...
        break;
        case CON_PICK_RACE:
            ...
        break;
        ....  
    }
    Something similar to the above. The switch it in an infinite loop, and basicly, it just takes your current state and parses your input accordingly.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was simply stating my opinion.
    I have no problem with you stating your opinion, but please don't force it on others. That tends to hurt more than help, much like teachers forcing students to use bad habits because they are just fine in the teachers opinion.

    >Ok Prelude, so there is something that it's possible to do with gotos and impossible in another way?
    Not that I know of, but there are times where a goto is a cleaner and more efficient solution that the "proper" structured approach.

    >I read in a document of an pro programmer and speaking about why we should avoid using gotos
    Probably Dijkstra's original rant "Go To Statement Considered Harmful". You can no doubt google it with ease since everyone likes to wield it as a weapon.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed