Manual:DIL Manual/atoi()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:17, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

atoi()

Function (integer). The number written at the start of a string.

atoi(s : string) : integer

Reads a whole number from the start of s, the C way: spaces before it are skipped, a + or - sign is allowed, and reading stops at the first character that isn't a digit.

Parameters

Name Type Meaning
s string The text to read, such as a word the player typed.

Returns

The number. A string that doesn't start with a number gives 0: atoi("abc") and atoi("") are 0, atoi("12 apples") is 12, atoi("-5") is -5. A string variable that has never been given a value makes the call fail, and the variable assigned keeps its old value.

Notes

  • Since a non-number gives 0, check the text when 0 is a valid answer.
  • Numbers beyond the integer range (about ±2.1 billion) don't come out right.

Example

dilbegin do_count(arg : string);
var
   w : string;
   n : integer;
code
{
   w := getword(arg);
   n := atoi(w);
   if ((n < 1) or (n > 100))
   {
      sendtext("Count to what? A number from 1 to 100.<br/>", self);
      quit;
   }
   sendtext("You count to " + itoa(n) + ".<br/>", self);
   quit;
}
dilend

A player command that reads a number from what was typed.

See also