Clear Filters
Clear Filters

How do i keep adding to my script?

1 view (last 30 days)
Swapnil srivastava
Swapnil srivastava on 26 Apr 2017
Edited: Walter Roberson on 26 Apr 2017
% the script asks for and adds book to the library
A=input('What would you like to do?: ','s'); %promots the user
while strcmp(A,'add book')==1 %if 'A'is add book the computer will ask for details
Tit= input('Ask for title: ','s');
Aut=input('Ask for author: ','s');
No_pg=input('Ask for number of pages: ','s');
fprintf('%s,%s,%s have been added to the library \n',Tit, Aut, No_pg)
Ve=[Tit:Aut:No_pg];
A=input('What would you like to do?: ','s');% will keep asking until prompt is different
if strcmp(A,'list book')==1
fprintf('Title: %s \n',Tit)
fprintf('Author: %s \n',Aut)
fprintf('Number of pages: %s \n',No_pg)
elseif strcmp(A,'quit')==1
disp('Good bye')
else
disp('Invalid Input')
end
end
In this script If i prompt 'add book'in A it asks for title, author, number of pages of the book. If i prompt 'quit'it displays goodbye and that is the only two strings for now. I want the code to keep asking 'What would you like to do'and I keep 'add book' to it and after a while i prompt 'list book' which should list all the books I have types. So far I can only do one. Example if i prompt 'add book' and have title= Harry potter author =jk rowling no. of pages=132 and i keep adding books and after a while I prompt list book the code only displays the last book i added and not all of them

Answers (1)

Walter Roberson
Walter Roberson on 26 Apr 2017
Edited: Walter Roberson on 26 Apr 2017
index = index + 1;
Tit{index} = input('Ask for title: ','s');
Aut{index} = input('Ask for author: ','s');
...
for J = 1 : index
fprintf('Title: %s \n',Tit{J})
fprintf('Author: %s \n',Aut{J})
...
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!