Home > Software design >  How to add user input to a list in Prolog then be able to view the list later
How to add user input to a list in Prolog then be able to view the list later

Time:05-20

"This is my code, I have no idea how to put the data into the 'student_list' so that it will be saved there and then I can search for it later." Anyone knows how to do this?

main:-  
    nl, put_char('*'), put_char('M'), put_char('A'), put_char('I'), put_char('N'), put_char(' '),
    put_char('M'),put_char('E'),put_char('N'),put_char('U'), put_char('*'), nl, nl,
    
    write('Enter number of option you wish to do.'),nl,
    write('[1] Add New Student'),nl,
    write('[2] Search Student Record'),nl,
    write('[3] EXIT'),nl,
    write('Option : '),
    read(X), nl, answer(X).
    answer(1):- add.
    answer(2):- search.
    answer(3):- put_char('B'), put_char('Y'), put_char('E'), put_char('!'), nl, write('Program Terminated.'), !.
    
add:-
    nl, put_char('*'), put_char('A'), put_char('D'), put_char('D'), put_char(' '), put_char('S'), put_char('T'),
    put_char('U'),put_char('D'),put_char('E'), put_char('N'),put_char('T'), put_char('*'), nl, nl,
    
    write('Enter ID Number :'),tab(1),read(Idnumber),
    write('Enter First Name :'),tab(1),read(Firstname),
    write('Enter Last Name :'),tab(1),read(Lastname),
    write('Enter Course :'),tab(1),read(Course),
    write('Enter Year :'),tab(1),read(Year),
    write('Enter Section :'),tab(1),read(Section),
    write('Enter number of subjects: '),read(Subjects),
        
    nl,
    write('Student Profile:'),nl,nl,
    write('ID Number :'),tab(1),write(Idnumber),nl,
    write('Name :'),tab(1),write(Lastname),write(', '),write(Firstname), nl,
    write('Course :'),tab(1),write(Course), nl,
    write('Year and Section :'),tab(1),write(Year),write(' - '),write(Section), nl,
    write('Number of Subjects: '), write(Subjects), nl,
    Total is Subjects*5000,
    write('Total Payment for Subjects Taken: '), write(Total), nl,
    
    
    student_list(Idnumber,Firstname,Lastname,Course,Year,Section,Subjects,Total),nl,
    
    nl, write('Add New Student? [yes/no]: '), read(Y), answer2(Y).
    answer2(yes):- add.
    answer2(no):- main.
    
search:-
    nl, put_char('*'), put_char('S'), put_char('E'), put_char('A'), put_char('R'), put_char('C'), put_char('H'),
    put_char(' '),put_char('S'),put_char('T'), put_char('U'),put_char('D'), put_char('E'), put_char('N'),
    put_char('T'),put_char('*'), nl, nl,    
    
    write('Enter Id Number of Student: '),
    read(Input),nl,
    
        
    student_list(Input,Fn,Ln,Co,Ye,Sec,Sub,Tot),nl,
    
    write('Student Profile: '), nl,
    write('Name :'),tab(1),write(Ln),write(', '),write(Fn), nl,
    write('Course :'),tab(1),write(Co), nl,
    write('Year and Section :'),tab(1),write(Ye),write(' - '),write(Sec), nl,
    write('Number of Subjects: '), write(Sub), nl,
    write('Total Payment for Subjects Taken: '), write(Tot), nl,
        
    nl, write('Search Again? [yes/no]: '), read(Z), answer3(Z).
    answer3(yes):- search.
    answer3(no):- main.

CodePudding user response:

You could use assertz to insert a fact into the Prolog database:

assertz(student_list(Idnumber,Firstname,Lastname,Course,Year,Section,Subjects,Total))

(NB. you may need to use the directive :- dynamic(student_list/8). at the top of the code if you put any student_list(...) examples in your source code).

  • Related