Search code examples
prologprolog-toplevel

how to get user input prolog and store it


I'm trying to have the user input their birthday so I can tell them their zodiac sign. However, I'm having trouble getting their actual birthday and month. Could someone please help me? I've tried separating the reads into different functors, but I keep getting errors. The error I get when I combine both reads is "Syntax error: Operator priority clash." The error I get when I separate both reads is "ERROR: =:=/2: Arguments are not sufficiently instantiated."

Code when I combine the reads:

start :-
   read_month,

read_month :-
   write('Enter your birth month (month followed by a .): '),
   nl,
   read(X),
   write('Enter your day of birth (followed by a .): '),
   nl,
   read(Y),
   horoscope(X,Y).

Code when I separate the reads:

start :-
   read_month,
   read_day.

read_month :-
   write('Enter your birth month (month followed by a .): '),
   nl,
   read(X).

read_day :-
   write('Enter your day of birth (followed by a .): '),
   nl,
   read(Y),
   horoscope(X,Y).

Solution

  • As a beginner, do not start like that. (And your first program is syntactically incorrect ; the first rule does not end with a period).

    Instead, write a relation month_day_sign/3 which you can use directly at the toplevel, like so:

    ?- month_day_sign(7,24,Sign).
       Sign = leo.
    

    That's the way how you normally interact with Prolog. So you are exploiting the functionality of the toplevel shell.

    Once you have mastered this, you may make some extra interface around. But don't go the other way!


    By separating both "actions", the X is now disconnected from horoscope(X,Y)...