[SOLVED] Trouble converting string "+" in an operator

Discussion in 'Programming' started by Taddio, Mar 15, 2019 at 3:42 PM.

  1. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    289
    I'm a bit confused, I have this pretty simple piece of code to create a random math formula, but I'm missing the right function to get the operators working.
    Code:
    var _num1 = irandom(5):
    var _num2 = irandom(10)
    var _oper = choose(" + ", " - ");
    
    var _formula = string(_num1)+_oper+string(_num3);
    
    var _answer = _num1 + ?????(_oper) + _num3;
    real(_oper) doesn't work (that was expected, as + is not a number), but I just can't find the right function to convert it to an actual operator.
    Any ideas are very welcome.
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    785
    GMS does not natively support evaluating expressions from strings, only number conversion. Either write if statements that use the operator to decide what to do with each side, or investigate the use of marketplace offerings like https://marketplace.yoyogames.com/assets/4227/n-string-parser-2 if you don't want to deal with the hassle yourself (if you will have much more complex expressions and this was just an example).
     
  3. KPJ

    KPJ Member

    Joined:
    Jan 19, 2019
    Posts:
    87
    Taddio if you do not find a way, you could use an if statement to check whether _oper is equal to "+" or "-". Depending on what it is, set _answer equal to num1 + num2 or num1 - num2. Hope this helps :)
     
  4. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    289
    Yeah, I've just been told it's impossible to convert a string in an operator in GM, so...guess I'll just have to deal with it the ugly way:D

    @chamaeleon will definately check it out, I may find inspiration in it for sure! Thanks

    I won't have more than 3-4 numbers max, and 2-3 operators (+-*/) but that becomes a lot quite fast in terms of possibilities nonetheless.
     
  5. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    785
    Just be aware that using an extension written in GML for this purpose won't be execute nearly as fast as a compiled expression would. It's possible the extension support some kind of caching of work to allow for the execution multiple times with changing variables, but not having used it myself, I couldn't say.. In any case, just something to keep in the back of your head if you try to use it in a heavy computation part of your code.
     
    Taddio likes this.
  6. SnotWaffle Studios

    SnotWaffle Studios Member

    Joined:
    Jun 21, 2016
    Posts:
    268
    I got it to work by changing your code a little bit:
    Code:
    var _num1 = irandom(5);
    var _num2 = irandom(10);
    var _oper = choose("+", "-");
    
    var _formula = string(_num1)+" "+_oper+" "+string(_num2);
    
    var _answer = _num1 + real(string(_oper)+"1")*_num2;
    
    show_debug_message(_formula+" = "+string(_answer));
    
    If you add more operators, just use an if or switch statement to account for the different kind of calculations.
     
    Taddio likes this.
  7. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    289
    Well, it took a total of 5 minutes to get working.
    I did a script scr_math_question(_num1, _num2, _num3, _oper1, _oper2); used a switch(argument4) within each cases of switch(argument3), and simply used (e.g) return argument0+(argument1*argument2) in the "*" case of arg4 within the "+" case of arg3

    Took 75 lines of codes, but mostly copy and pasted, so it's all good (tho I'll remove the division to avoid the div by zero hassle)

    Wow, really?!? I will have to check that out! Nicely done
     
    SnotWaffle Studios likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice