Issues with reading/using "*" and "#" in a string
이전 댓글 표시
I'm building a simple tone dialer for a lab, and I thought it would be cool to evaluate the user's input string into an array, and then evaluate each piece of that array so I can assign it to a sound that plays back to the user. Right now I've successfully been able to read and spit out the string with some accuracy, but when I join "*" or "#" with another number it seems to be messing with the ASCII value because it exits right out of my IF statement. I'll post my code as I have it right now, thanks in advance for any help.
P.S. This is very incomplete, just trying to make sure I can reasonably validate an input
-------------------------------------------------------------------------------------------------------
token1 = 0;
while token1 == 0
input2 = input('Please enter a 7 digit phone number: ','s');
[number, token1] = getNumber(input2);
end
----------------------------------------------------------------
function [out2,token] = getNumber(y)
inputNum = num2str(y) - '0';
for i = 1:length(inputNum)
if inputNum(i) >= 0 & inputNum <= 9 | inputNum == -6 | inputNum == -13
switch inputNum(i)
case 0
disp(inputNum(i))
case 1
disp(inputNum(i))
case 2
disp(inputNum(i))
case 3
disp(inputNum(i))
case 4
disp(inputNum(i))
case 5
disp(inputNum(i))
case 6
disp(inputNum(i))
case 7
disp(inputNum(i))
case 8
disp(inputNum(i))
case 9
disp(inputNum(i))
case -6
fprintf("*%c",newline);
case -13
fprintf("#%c",newline);
end
else
fprintf("That is not a valid phone number, please enter a valid number.%c",newline);
token = 0;
break
end
token = 1;
end
out2 = 0;
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!