Skip to content
العربية

3. Input

To read a line the user types, and convert it to a number.

  • اقرأ("رسالة") (read) shows the message, then reads a line the user types. Its result has the type نص؟ (optional string): a string, or لاشيء (nothing) at the end of input. So give it a fallback: اقرأ("رسالة") والا "".
  • حلل(النص, عدد64) (parse) converts a string to an integer. Its result has the type !عدد64, meaning the conversion may fail, so you handle the failure where it happens.

The handbook’s examples run with no input, so اقرأ returned لاشيء here, and والا "" gave an empty string:

Example
اسم = اقرأ("ما اسمك؟ ") والا ""
اطبع("أهلاً ", اسم)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
ما اسمك؟ أهلاً 

حلل(النص, عدد64) has the type !عدد64, because what the user types may not be a number. So give a fallback value for the failure: حلل(اقرأ() والا "", عدد64) والا 0.

Causes the error
اطبع(حلل("ثلاثة", عدد64) + 1)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
​[خطأ دلالي E0399] سطر 1:6 - 'حلل(...)' من النوع '!عدد64' (قد يفشل) ولا يُستعمل كأنه 'عدد64'   1 | اطبع(حلل("ثلاثة", عدد64) + 1)     |      ^~~  تلميح: عالج الخطأ في موضعه:  جرّب حلل(...)  (يمرّر الخطأ إلى مستدعي الدالة)   أو   حلل(...) والا قيمة_بديلة   أو   حلل(...) والا خ:  ثم كتلة تعالج «خ»

Exit code: 2

In the error catalog: E0399

Try this example in the playground

Fixes it
اطبع((حلل("ثلاثة", عدد64) والا 0) + 1)
ن = حلل("٣", عدد64) والا خ:
اطبع("ليس عدداً: ", خ)
0
اطبع(ن)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
13

Try this example in the playground

Read a number from the user and print twice its value; if what they typed is not a number, treat it as 0 with والا (else). (In the solution, the user typed 21.)

Show the solution
Solution
ن = حلل(اقرأ("اكتب عدداً: ") والا "", عدد64) والا 0
اطبع("ضعفه: ", ن * 2)
Output recorded from the compilermeem 03_حل.م · meem 0.17.3+434e13f
Input
21
اكتب عدداً: ضعفه: 42

Builtins: اقرأ and حلل · Error handling