How can I make this program accept input 'value' as only a positive number except for the case 1(where i would like to have both negative and positive numbers as input)?

The input 'value' only takes integers to proceed(it keeps asking for input until the user inputs only a number) but i want the the input 'value' to accept only positive numbers for all cases under the switch y except for the case 1(i.e. I want case 1 to accept both positive and negative numbers)
a = input('Enter the num of your choice between 1 to 4 ');
while true
value = input('Enter any value that you want to convert:','s');
disp(' ')
if ~isnan(str2double(value))
value=str2double(value);
break;
else
disp(' ')
disp('Wrong type of value entered. Try again.');
disp('HINT:Make sure to Enter input in numbers.')
disp(' ')
end
end
switch a
case 1
R=value*5
case 2
R= value * 6
case 3
R = value*7
case 4
R = value*8
otherwise
end

Answers (1)

if a == 1
    R = value*5;
else
    if value<0
        disp('Value must be positive')
    else
        R = value*(a+4);
    end
end
disp(R)

Categories

Answered:

on 16 Apr 2018

Community Treasure Hunt

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

Start Hunting!