abduls85
abduls85

Reputation: 548

Prolog : Simple question

I want to add any strings user entered into a list

run :- write('How many students you have: '),read(x),nl.
       enterNameOfStudents(x).

enterNameOfStudents(x) :- for(A, 1, x, 1),write('Please enter the names of students'),read(A),??????,nl,fail.

What do i put in the ?????? portion to ensure that whatever the user enter will go into a user-defined list which will be used for further processing later ? Please help. I have tried numerous stuff like append and other but it does not work :(

Upvotes: 3

Views: 213

Answers (1)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

enterNameOfStudents(0, Names):-!.
enterNameOfStudents(X, [N|Rest]) :-    write('Enter a name: '), read(N), nl,
                   X1 is X - 1, enterNameOfStudents(X1, Rest).

run(Names) :- write('How many students you have: '),read(X),nl,
   enterNameOfStudents(X, Names).

You can construct the list recursively like this. You need to pass an argument to run so that you get back the full list at the end.

Upvotes: 1

Related Questions