Difference between revisions of "Manual:DIL Manual/atoi()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = atoi() = | |
| − | + | '''Function''' (integer). The number written at the start of a string. | |
| − | + | ||
| − | + | atoi(s : string) : integer | |
| − | + | ||
| − | < | + | Reads a whole number from the start of <code>s</code>, the C way: spaces before it are skipped, a <code>+</code> or <code>-</code> sign is allowed, and reading stops at the first character that isn't a digit. |
| − | + | ||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| − | + | ! 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: <code>atoi("abc")</code> and <code>atoi("")</code> are 0, <code>atoi("12 apples")</code> is 12, <code>atoi("-5")</code> 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 == | ||
| + | <pre> | ||
| + | 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 | ||
| + | </pre> | ||
| + | A player command that reads a number from what was typed. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/itoa()|itoa()]] | ||
| + | * [[Manual:DIL Manual/getword()|getword()]] | ||
Latest revision as of 23:17, 14 November 2026
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.