Author Topic: Another comma_int function  (Read 3824 times)

Offline Jimorie

  • Friend
  • **
  • Posts: 58
    • View Profile
Another comma_int function
« on: October 24, 2006, 02:08:46 am »
Just posting an alternative solution to the one posted by Llyr in this thread: http://lpmuds.net/forum/messages.php?webtag=LPC&msg=178.1">http://lpmuds.net/forum/messages.php?webtag=LPC&msg=178.1



Edit: Fixed to actually compile on MudOS. ;)



code:
string comma_int (int n)
{
    string str;
    int i;
   
    str = itoa(n);
   
    for (i = strlen(str) - 3; i > (n < 0); i -= 3)
    {
        str = str[0 .. (i - 1)] + "," + str[i .. (strlen(str) - 1)];
    }
   
    return str;
}

Offline capo

  • Friend
  • **
  • Posts: 70
    • View Profile
Another comma_int function
« Reply #1 on: October 24, 2006, 07:47:49 am »
I worked around with this for a while and was unable to get it to function, I was thwarted in the end by a couple of errors that I could not get rid of:



quote:
> update comma_int.c

/secure/sefun/comma_int.c line 6: Bad assignment ( string vs int ).

   

/secure/sefun/comma_int.c line 10: syntax error

   

---

*Error in loading object '/secure/sefun/comma_int'

Object: /secure/sefun/sefun at /secure/sefun/load_object.c:14



'<function>' at /secure/save/creators/c/capo (<function>) at /:0

'cmdAll' at /secure/save/creators/c/capo (/lib/command.c) at line 93

'cmd' at /secure/cmds/creators/update at line 109

'eventUpdate' at /secure/cmds/creators/update at line 135

'load_object' at /secure/sefun/sefun at /secure/sefun/load_object.c:14

'CATCH' at /secure/sefun/sefun at /secure/sefun/load_object.c:14

Trace written to /log/catch

/secure/sefun/comma_int.c line 6: Bad assignment ( string vs int ).

   

/secure/sefun/comma_int.c line 10: syntax error

   

---

*Error in loading object '/secure/sefun/comma_int'

Object: /secure/cmds/creators/update at line 148



'<function>' at /secure/save/creators/c/capo (<function>) at /:0

'cmdAll' at /secure/save/creators/c/capo (/lib/command.c) at line 93

'cmd' at /secure/cmds/creators/update at line 109

'eventUpdate' at /secure/cmds/creators/update at line 148

'CATCH' at /secure/cmds/creators/update at line 148

Trace written to /log/catch

/secure/sefun/comma_int: Error in update

*Error in loading object '/secure/sefun/comma_int'

Offline capo

  • Friend
  • **
  • Posts: 70
    • View Profile
Another comma_int function
« Reply #2 on: October 24, 2006, 07:59:34 am »
This gets rid of the syntax error but not the assignment string vs int error:



code:
string comma_int (int n)
{
    string str;
    int i;

    str = atoi(n);

    for (i = strlen(str) - 3; i > (n < 0); i -= 3)
    {
        str = str[0 .. (i - 1)] + "," + str[i .. strlen(str)];
    }

    return str;
}

Offline daelaskai

  • BFF
  • ***
  • Posts: 174
    • View Profile
Another comma_int function
« Reply #3 on: October 24, 2006, 08:55:45 am »
You are getting a assignment error because you are defining "str" as a string.  atio(n) is turning the string "n" and changing it to an integer.  litterally, to me, atoi translates to "alpha-to-integer".



Thus string str != int n.



In order to change this, have it use itoa(n) instead of atoi(n).  Again, to me, it translates to "integer-to-alpha".



code:

string comma_int (int n)
{
    string str;
    int i;

    str = itoa(n);

    for (i = strlen(str) - 3; i > (n < 0); i -= 3)
    {
        str = str[0 .. (i - 1)] + "," + str[i .. strlen(str)];
    }

    return str;
}




Have fun folks :)



Daelas



P.S.  I hope this doesn't sound like a LPC lecture to people who know what atoi or itoa is.  I made the comments at the top for people who don't really know what these two things are.  If anyone does know already, please disregard that portion. :)

Offline Jimorie

  • Friend
  • **
  • Posts: 58
    • View Profile
Another comma_int function
« Reply #4 on: October 24, 2006, 11:30:24 am »

Ah you are correct of course. I threw the itoa() function in there because I thought that was how it was done on MudOS - and of course got the wrong one. In the code I tested I used str = (string)i; which is the DGD way to do it. Sorry for that mistake.



And I didn't know MudOS don't accept the str[i..] or str[..i] syntax. I'll keep that in mind.



I edited the original post to fix these errors. Thanks folks!