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)?
Show older comments
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
1 Comment
KSSV
on 16 Apr 2018
Put a condition:
if a<=0
fprintf('Please input +ve number')
end
Answers (1)
Are Mjaavatten
on 16 Apr 2018
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
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!