## switch statements and trig function

### Marina Christakos (view profile)

on 24 Jan 2019
Latest activity Edited by Kevin Phung

### Kevin Phung (view profile)

on 24 Jan 2019
val = str2double(input('enter number','s'));
chr = input('enter letter','s');
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
I wrote this code to find the sin,cos,or tan of an entered individual number. I now need to modify it so that instead of reading a single character/number, the program continually reads character/number pairs and displays the trigonometry result. If a number outside the range [-100, 100] is entered, the program should not display the trigonometry result and should ask for another character/number pair to be entered. I can't figure out how to make the initial modifications of reading in pairs - any shortcuts I can use?

Kevin Phung

### Kevin Phung (view profile)

on 24 Jan 2019
what do you mean by reading in pairs?
Marina Christakos

### Marina Christakos (view profile)

on 24 Jan 2019
instead of the program asking for a letter and number separately, I as the user am able to write c90 and the output will respond with 0

### Kevin Chng (view profile)

on 24 Jan 2019

I think the most simple approach is to add another loop which is
val = str2double(input('enter number','s'));
chr = input('enter letter','s');
if val>=-100 && val<=100
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
warning('enter new input, your input is out of range')
end

Marina Christakos

### Marina Christakos (view profile)

on 24 Jan 2019
that still makes me input the number and letter at separate times
Kevin Chng

### Kevin Chng (view profile)

on 24 Jan 2019
I might miss understanding your question.
However, it is still input them in separate line, if wants to input in the same line, later, you might need to separate them since one is string/char and the other one is numeric
clc;clear all;
chr = input('enter letter & number :','s');
% Need some logic to separate them and ensure the user key in the right thing
C = {'1','2','3','4','5','6','7','8','9','0'};
for i = 1:10
Index = strfind(chr, C{i});
if isempty(Index)~=1
if exist('thefirstdigit')==0
thefirstdigit=Index;
else
if thefirstdigit>Index
thefirstdigit=Index;
end
end
end
end
%indexing to chr, then separate them into val and chr2
val = str2double(chr(thefirstdigit:end));
chr2 = (chr(1:thefirstdigit-1));
if val>=-100 && val<=100
switch chr2
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
Run the program and enter :s100

### Kevin Phung (view profile)

on 24 Jan 2019
Edited by Kevin Phung

### Kevin Phung (view profile)

on 24 Jan 2019

you can just run the input once, and evaluate the input as a whole.
word = input('enter letter followed by number','s');
if numel(word)>1
chr = word(1);
val =str2num(word(2:end))
if isempty(val) || val >100 || val < -100 %occurs when 2nd character is a letter and not a number
disp('Try again')
return
end
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
disp('Try again')
end