How do i keep adding to my script?

1 次查看(过去 30 天)
% 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

回答(1 个)

Walter Roberson
Walter Roberson 2017-4-26
编辑:Walter Roberson 2017-4-26
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by