'Prolog is giving random numbers instead of string in read write command

say_hi:-
    write('Your name? '),
    read(X),
    write('hi'),
    write(X).

THIS IS MY CODE BUT!!! THIS HAPPEN WHEN I RUN IT. WHY?

?- say_hi.
Your name? Ali.
hi_884
true.

It's not giving

> hi Ali

. WHY?

please guide me and give me simple code to read and write name. every time put different name it gives different numbers.



Solution 1:[1]

This happens because read/1 is meant to parse Prolog terms, and a sequence of letters starting with an uppercase letter is a variable. The strange _NNN output is - conventionally - the way Prolog display an uninstantiated variable. In SWI-Prolog you can use read_line_to_codes/2 or read_line_to_string/2 to get your string. For example

?- read_line_to_string(user_input,S).
|: Ali
S = "Ali".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1