Thread: Newbie question: Help with these if statements?

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

    Newbie question: Help with these if statements?

    The following program i have write but i get a problem.

    If i use single if for each team and whatever string i write it gets values from the last team.

    if i use else if i get values from the first team.

    How i shall write it to get the potion value from the right team for the string i type.
    Something i miss, but i cant figure it out

    Code:
    void do_create_potion( CHAR_DATA* ch, const char* argument)
    {
       OBJ_DATA *ingredient;
       OBJ_DATA *potion;
       int selection;
       int dice;
       int check;
    
    
       dice = number_percent();
       check = LEARNED(ch, gsn_create_potion) + (get_curr_int(ch)/2);
      
       ingredient = get_obj_carry(ch, argument);
    
    
       selection = rand() %3;
    
    
       if(argument[0] == '\0' || !str_cmp(argument, ""))
         {
         send_to_char( "Brew what?\r\n", ch );
         return;
         }
    
    
         if(str_cmp(argument, "alit hide"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_INTELLIGENCE), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_POISON_RESISTANCE), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_PICKPOCKET), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_DETECT_INVISIBILITY), 0);}
           }
    
    
         if(str_cmp(argument, "ampoule pod"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_WATER_BREATHING), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_PARALYSIS), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DETECT_INVISIBILITY), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_WILLPOWER), 0);}
           }
    
    
         if(str_cmp(argument, "ash salts"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_AGILITY), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_MAGIC_RESISTANCE), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DISEASE_IMMUNITY), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_RESTORE_MAGICKA), 0);}
           }
    
    
         if(str_cmp(argument, "ash yam"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_INTELLIGENCE), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_STRENGTH), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DISEASE_RESISTANCE), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_SNEAK), 0);}
           }
    
    
         if(str_cmp(argument, "bittergreen petals"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_RESTORE_INTELLIGENCE), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_INVISIBILITY), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_ENDURANCE), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_MAGICKA), 0);}
           }
    
    
         if(str_cmp(argument, "black anther"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_AGILITY), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_FIRE_RESISTANCE), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_ENDURANCE), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_ALCHEMY), 0);}
           }
    
    
         if(str_cmp(argument, "black lichen"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_STRENGTH), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_FROST_RESISTANCE), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_AGILITY), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_POISON_IMMUNITY), 0);}
           }
    
    
         if(str_cmp(argument, "bloat"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_MAGICKA), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_INTELLIGENCE), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_WILLPOWER), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_DETECT_INVISIBILITY), 0);}
           }
    
    
         if(str_cmp(argument, "bonemeal"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_RESTORE_AGILITY), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_PICKPOCKET), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_FATIGUE), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_PERSONALITY), 0);}
           }
    
    
         if(str_cmp(argument, "corkbulb root"))
           {
         if(selection == 0)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_AGILITY), 0);}
         if(selection == 1)
           {potion = create_object(get_obj_index(OBJ_POTION_FIRE_RESISTANCE), 0);}
         if(selection == 2)
           {potion = create_object(get_obj_index(OBJ_POTION_DRAIN_ENDURANCE), 0);}
         if(selection == 3)
           {potion = create_object(get_obj_index(OBJ_POTION_FORTIFY_ALCHEMY), 0);}
           }
    
    
     if(check < dice)
       {
       if(ingredient != NULL)
         {
         ch_printf( ch, "You brew a %s.\r\n", potion->name); 
         obj_to_char(potion, ch);
         extract_obj(ingredient);
         learn_from_success(ch, gsn_create_potion);
         }
       else
         {
         send_to_char( "You don't have this ingredient.\r\n", ch );
         }
       }
     else
      {
      ch_printf( ch, "You failed brewing.\r\n"); 
      extract_obj(ingredient);
      }
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    You might find it more maintainable to put the data into tables in the form of arrays that you then iterate over. For example:
    Code:
    static const int obj_indices[][4] = {
        {OBJ_POTION_DRAIN_INTELLIGENCE, OBJ_POTION_POISON_RESISTANCE, OBJ_POTION_FORTIFY_PICKPOCKET, OBJ_POTION_DETECT_INVISIBILITY}, // alit hide
        {OBJ_POTION_WATER_BREATHING, OBJ_POTION_PARALYSIS, OBJ_POTION_DETECT_INVISIBILITY, OBJ_POTION_DRAIN_WILLPOWER},               // ampoule pod
        {OBJ_POTION_DRAIN_AGILITY, OBJ_POTION_MAGIC_RESISTANCE, OBJ_POTION_DISEASE_IMMUNITY, OBJ_POTION_RESTORE_MAGICKA}              // ash salts
    };
    
    static const char *ingredient_names[] = {
        "alit hide", "ampoule pod", "ash salts"
    }
    
    if (argument[0] == '\0' || strcmp(argument, "") == 0)
    {
        send_to_char("Brew what?\r\n", ch);
        return;
    }
    
    selection = rand() % 3;
    
    potion = NULL;
    for (size_t i = 0; i < sizeof(ingredient_names) / sizeof(ingredient_names[0]); ++i)
    {
        if (strcmp(argument, ingredient_names[i]) == 0)
        {
            potion = create_object(get_obj_index(obj_indices[i][selection]), 0);
            break;
        }
    }
    Unless argument has domain-specific meaning in this apparent game of yours, you should not use it as a parameter name. From what I see, ingredient_name might be a more descriptive name for that parameter.

    Also, instead of rand() % 3, you probably want to write rand() % 4, and that 4 should be a named constant that's also used to define obj_indices.

    I have changed your use of str_cmp to strcmp; if you really do have your own str_cmp function that is preferable for some reason you can of course switch it back. I suspect that your str_cmp returns a boolean true for equal, in which case you might want to rename str_cmp to something like equal_str because str_cmp can easily be confused with strcmp that has very different semantics, and then there is some issue with the str prefix being reserved to the implementation.
    Last edited by laserlight; 04-13-2020 at 08:57 PM.
    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
    4
    Hello friend, thanks for answer.
    It compiles without errors but it crashes when i try to brew a potion (run the script)

    By my little knowdge this happens because of
    if (strcmp(argument, ingredient_names[i]) == 0)

    This requires the excact word to be written. Because this is made for an multiplayer game, and maybe not everyone will write the whole word and it cause the program to crash.

    For example if i type brew ampoule pod it does the work as intented, but if i write brew ampoule it crashes.
    Last edited by oblisgr; 04-14-2020 at 09:11 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    That's why I set potion to be NULL just before the loop. This gives you a way to check if the potion was brewed at all. If it wasn't (i.e., it remains NULL after the loop), you would find a way to inform the player to try again instead.
    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. C Programming If statements Help (Newbie sorry)
    By julia888 in forum C Programming
    Replies: 8
    Last Post: 02-26-2018, 03:23 AM
  2. Question with If-then Statements?
    By BLG in forum C Programming
    Replies: 1
    Last Post: 09-08-2009, 09:18 PM
  3. Question About Or Statements
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2009, 01:44 PM
  4. While Statements Question
    By thekautz in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 02:48 PM
  5. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM

Tags for this Thread