1

I have to write a simple expert system in Prolog for scheduling of classes. In this code part, I want that user add an instructor to system. For this, reading two input value but I am getting this error.

addinstructor() :-    
    read(id),
    read(courseid),     
    assert(instructor(id, courseid)),    
    write("added").

Query:

?- addinstructor().
5
cse102.    

Then, I am getting operator expected error. How do i fix this to work my code?

leiaorgana
  • 13
  • 3
  • 1
    Hi, I will recommend reading [Ask]. Tell us more about what is the prupuse, the expected result , the context, the input, the output. Everything that make sense for your question. – Drag and Drop Dec 14 '18 at 14:39
  • There is no need for the empty parentheses after `addinstructor`, either in the definition or in the query that calls it. I would recommend you remove them. Prolog variables **MUST** be capitalized; `id` and `courseid` are *atoms*, not variables, and this is a problem for your code. Also, `assert/1` is non-standard, please use `asserta/1` or `assertz/1`, which give you control over the order of the facts in the database. – Daniel Lyons Dec 14 '18 at 18:56
  • Thank you for your help @Daniel Lyons . Problem solved. – leiaorgana Dec 16 '18 at 11:57

1 Answers1

2

The predicate read/1 reads Prolog terms not raw data. Prolog terms end with a period.

So if instead of entering 5 if you enter 5. you will not get the error.

Instead of using the predicates found in Term reading and writing, e.g. read/1, you should use the predicates in Primitive character I/O for reading characters or Predicates that operate on strings for reading strings, e.g. read_string/3

To answer your next question of how do I implement this, see Prolog - Write out facts and reading a users input and then this.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136