# switch statements and trig function

21 views (last 30 days)
Marina Christakos on 24 Jan 2019
Edited: Kevin Phung 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?
##### 2 CommentsShowHide 1 older comment
Marina Christakos on 24 Jan 2019
Edited: Marina Christakos 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 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
##### 2 CommentsShowHide 1 older comment
Kevin Chng 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 on 24 Jan 2019
Edited: Kevin Phung 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

### Categories

Find more on Characters and Strings in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by