tags:

views:

66

answers:

2

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 :(

+1  A: 
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.

Vincent Ramdhanie
A: 

Thanks a lot for your help. I have solved the problem.