Thread: Can i build a shorter database?

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    9

    Exclamation Can i build a shorter database?

    Hello, i m newbie and i want to ask if there a way to make my databale shorten and more smart. To be not nessessary every time to type all those ifs.

    An example would be perfect.
    Thanks


    Code:
    if(event == 1)
          {
          sprintf(mob_name,  "Leles Birian");
          sprintf(mob_sex,   "female");
          sprintf(mob_race,  "breton");
          sprintf(mob_class, "mage");
          mob_level = 25;
          mob_ac    = 5;
          mob_thac0 = 11;
          }
            
        if(event == 2)
          {
          sprintf(mob_name,  "Nels Llendo");
          sprintf(mob_sex,   "male");
          sprintf(mob_race,  "dunmer");
          sprintf(mob_class, "thief");
          mob_level = 25;
          mob_ac    = 5;
          mob_thac0 = 11;
          }
    
    
        if(event == 3)
          {
          sprintf(mob_name,  "Nevrasa Dralor");
          sprintf(mob_sex,   "female");
          sprintf(mob_race,  "dunmer");
          sprintf(mob_class, "pilgrim");
          mob_level = 25;
          mob_ac    = 5;
          mob_thac0 = 11;
          }
    
    
          ch_printf(ch, "&PI am fighting &R%s&P, the %s %s %s.&D\r\n", mob_name, mob_sex, mob_race, mob_class);
     
          if((player_thac0 - player_dice) < mob_ac)
            { 
            ch_printf(ch, "\r\n");
            ch_printf(ch, "&PThe arena battle is over and i won.&D\r\n");
            experience = (mob_level * 100);
            ch->exp = ch->exp + experience;
            ch_printf(ch, "&PI earned &R%d&P exp.&D\r\n", experience);
    
    
            for(size_t i = 0; i < sizeof(loot) / sizeof(loot[0]); ++i)
               {
               item = create_object(get_obj_index(loot[i][selection]), 0);
            break;
               };
    
    
            obj_to_char(item, ch);
            ch_printf(ch, "&PI got a &R%s&P.&D\r\n", item->name);
            }
          else if((mob_thac0 - mob_dice) < player_AC)
            {
            ch_printf(ch, "\r\n");
            ch_printf(ch, "&PThe arena battle is over and i lost.&D\r\n");
            dmg = (number_range(1,mob_level) + 0)+(number_range(1,mob_level) + 0)+(number_range(1,mob_level) + 0);
            ch_printf(ch, "&PI suffered &R%d&P dmg.&D\r\n", dmg);
            global_retcode = damage(ch, ch, dmg, gsn_shock_damage);
            }
          else
            {
            ch_printf(ch, "&PWe both run away!&D\r\n");
            }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, one way is to use a database engine so you just enter the data, then given the event number you query the database for the exact data that you need. SQLite would be a possible option.

    Another way could be to define a struct type for these entities, then you create an array of struct objects. So, given an event number, you just select the array element corresponding to the event number. You can even make this more flexible by representing the data in some kind of text-based file format that is easy for you to populate and which you could say, use a parser library to parse into your array of struct objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    9
    like this?

    Code:
       const struct database[1] = 
          {
          {1,  Leles Birian, female, breton, mage , 25, 5, 11 },
          {2,  Nels Llendo , male  , dunmer, thief,  6, 6, 16 }
          };
    if yes how i ask to check it later?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a rough idea, but not quite. First you need to define a struct type:
    Code:
    struct mob
    {
        char name[NAME_MAXLEN + 1];
        char sex[SEX_MAXLEN + 1];
        char race[RACE_MAXLEN + 1];
        char class[CLASS_MAXLEN + 1];
        int level;
        int ac;
        int thac0;
    };
    Then possibly globally since it is const, or maybe in a function from which you pass it around as an argument, you construct the array:
    Code:
    const struct mob mobs[] = {
        {"Leles Birian", "female", "breton", "mage", 25, 5, 11},
        {"Nels Llendo", "male", "dunmer", "thief", 25, 1, 11},
        // ...
    };
    Now, with your event number that appears to be 1-indexed, after having checked that the event number is valid, you can just write:
    Code:
    int index = event - 1;
    ch_printf(ch, "&PI am fighting &R%s&P, the %s %s %s.&D\r\n", mobs[index].name, mobs[index].sex, mobs[index].race, mobs[index].class);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    9
    Yes that helped a lot, i had an idea how it could be done but didnt know how. i made it like this:

    Code:
    struct mob
          {
          char name[64];
          char sex[64];
          char race[64];
          char class[64];
          int level;
          int ac;
          int thac0;
          };
    
    
        const struct mob mobs[] = 
          {
          {"Leles Birian"   , "female", "breton", "mage"    , 25, 5, 11 },
          {"Nels Llendo"    , "male"  , "dunmer", "thief"   ,  6, 6, 16 },
          {"Levrasa Dralor" , "female", "dunmer", "pilgrim" ,  4, 5, 16 }
          };
    
    
          int index = event - 1;
          ch_printf(ch, "&PI am fighting &R%s&P, the %s %s %s.&D\r\n", mobs[index].name, mobs[index].sex, mobs[index].race, mobs[index].class);
    but i get error messages
    Attached Images Attached Images Can i build a shorter database?-untitled-jpg 
    Last edited by oblisgr; 11-19-2020 at 04:23 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    On a hunch: are you compiling with a C compiler or a C++ compiler? The .c file extension makes me think that it should default to C even if your compiler toolchain supports both C and C++, but I could be mistaken. You must compile C code with a C compiler unless you also know C++ and know what you're doing by writing C code that is valid C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    9
    Quote Originally Posted by laserlight View Post
    On a hunch: are you compiling with a C compiler or a C++ compiler? The .c file extension makes me think that it should default to C even if your compiler toolchain supports both C and C++, but I could be mistaken. You must compile C code with a C compiler unless you also know C++ and know what you're doing by writing C code that is valid C++.
    I m using gcc 9.3.0 in windows linux subsystem.
    I think those char variables cause the problem
    i used the following and it compiled:

    Code:
       struct mob
          {
          int level;
          int ac;
          int thac0;
          };
    
    
        const struct mob mobs[] = 
          {
          {25, 5, 11 },
          {6, 6, 16 },
          {4, 5, 16 }
          };
    
    
      if(is_affected(ch, gsn_dreamwalk))
        {
    
    
          int index = event - 1;
          ch_printf(ch, "&PI am fighting the %d %d %d.&D\r\n", mobs[index].level, mobs[index].ac, mobs[index].thac0);
    
    
        }
    Last edited by oblisgr; 11-19-2020 at 04:46 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that demonstrates the compile error.

    Alternatively, you can rename class to mob_class. If that fixes your compile errors, then you're probably accidentally invoking g++ instead of gcc, and maybe the case-insensitive nature of Windows file names is coming into play even with the Linux subsystem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2020
    Posts
    9
    Omg i changed those variable to mob_ and it compliled. i think class was crashing it.
    THANK YOU VERY MUCH FOR YOUR TIME

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, I'm afraid that you're compiling as C++, not C. You need to figure out how to change your setup to compile as C, otherwise you'll find that you can write valid C code that turns out to be invalid according to your compiler, not because it is invalid C, but because it is invalid C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can l make this shorter?
    By am687876 in forum C Programming
    Replies: 3
    Last Post: 05-28-2013, 11:12 AM
  2. shorter code
    By czarny020 in forum C Programming
    Replies: 10
    Last Post: 02-14-2011, 12:32 PM
  3. How to build a baseball stats database
    By Programmer3922 in forum C Programming
    Replies: 1
    Last Post: 08-01-2008, 11:01 PM
  4. Any way to make shorter???
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-14-2002, 04:02 AM
  5. is there a better/shorter way to do a menu
    By sizzle_chest in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2001, 04:09 PM

Tags for this Thread