How do i keep adding to my script?

조회 수: 3 (최근 30일)
Swapnil srivastava
Swapnil srivastava 2017년 4월 26일
편집: Walter Roberson 2017년 4월 26일
% 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에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by