How to find length of user input?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi everyone. I have a program here to determine ISBN numbers of a given 9 digit number. However, I am running into some difficulty.
clc
clear
isbn = input('Enter a nine digit ISBN number:' );
if length(isbn) == 9
d1 = str2num(isbn(1));
d2 = str2num(isbn(2));
d3 = str2num(isbn(3));
d4 = str2num(isbn(4));
d5 = str2num(isbn(5));
d6 = str2num(isbn(6));
d7 = str2num(isbn(7));
d8 = str2num(isbn(8));
d9 = str2num(isbn(9));
A = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9);
mod (A, 11);
d10 = mod;
if d10 ~= 10
fprintf ("ISBN-10: %f-%f%f-%f%f%f%f%f%f-%f",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
elseif d10 == 10
d10 = X;
fprintf ("ISBN-10: %f-%f%f-%f%f%f%f%f%f-%f",d1, d2, d3, d4, d5, d6, d7, d8, d9, X);
end
else if length(isbn) ~= 9
disp ('Error, ISBN must be exactly 9 digits.')
end
end
I am trying to get the length of the user input, but everytime I type in a 9 digit number, it reads me the Error display I wrote. It is not realizing the length value of "123456789" as 9 but rather as 1. Any suggestions?
댓글 수: 0
채택된 답변
Stephan
2019년 2월 26일
Hi,
a lots of bugs in this - i think i fixed them all:
clc
clear
isbn = input('Enter a nine digit ISBN number:' );
isbn = char(string(isbn));
if strlength(string(isbn)) == 9
d1 = str2double(isbn(1));
d2 = str2double(isbn(2));
d3 = str2double(isbn(3));
d4 = str2double(isbn(4));
d5 = str2double(isbn(5));
d6 = str2double(isbn(6));
d7 = str2double(isbn(7));
d8 = str2double(isbn(8));
d9 = str2double(isbn(9));
A = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9);
d10 = mod (A, 11);
if d10 ~= 10
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%d\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
elseif d10 == 10
d10 = 'X';
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%s\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
end
else
if length(isbn) ~= 9
disp ('Error, ISBN must be exactly 9 digits.')
end
end
Best regards
Stephan
댓글 수: 2
Stephan
2019년 2월 26일
편집: Stephan
2019년 2월 26일
clc
clear
isbn = input('Enter a nine digit ISBN number:', 's');
if strlength(isbn) == 9
d1 = str2double(isbn(1));
d2 = str2double(isbn(2));
d3 = str2double(isbn(3));
d4 = str2double(isbn(4));
d5 = str2double(isbn(5));
d6 = str2double(isbn(6));
d7 = str2double(isbn(7));
d8 = str2double(isbn(8));
d9 = str2double(isbn(9));
A = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9);
d10 = mod (A, 11);
if d10 ~= 10
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%d\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
elseif d10 == 10
d10 = 'X';
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%s\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
end
else
if length(isbn) ~= 9
disp ('Error, ISBN must be exactly 9 digits.')
end
end
should work
추가 답변 (1개)
emad xa
2020년 4월 6일
i run program have a problem
Undefined function or variable 'strlength'.
Error in Q3 (line 4)
if strlength(isbn) == 9
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!