# How can I ask the user to input units of either m or in, then have the program keep asking them for units until they input either m or in?

1 view (last 30 days)
Tanner Larkey on 13 Apr 2018
Commented: Tanner Larkey on 14 Apr 2018
Asks the user the unit of l. The program must accept the following units or keep asking for a unit: a. m b. in
bidyut mandi on 13 Apr 2018
Edited: bidyut mandi on 13 Apr 2018
Ask the user the unit of I means disp('Enter the unit of I').I don't get that the program everytime ask the unit of a.m,b.in or single time.Please clarify this

Walter Roberson on 14 Apr 2018
I suggest you use
iChoice = menu('Select a unit', 'inches', 'metres');
This will return 1 for inches and 2 for metres.
Tanner Larkey on 14 Apr 2018
Thanks! Using the menu makes things a lot easier! Here is what I came up with:
if units == 1
g = 384;
fprintf('Gravity is 384 in/s^2')
else
g = 9.81;
fprintf('Gravity is 9.81 m/s^2')
end

ES on 13 Apr 2018
disp('Choose the Unit.');
disp('[0] Inches.');
disp('[1] Metres.');
while(~isnumeric(iChoice) ||(uint8(iChoice)>1))
iChoice=input('Please make a valid choice. Enter 0 to quit. ');
end
Tanner Larkey on 14 Apr 2018
Then how would I write a script that correctly determines the value of gravitational acceleration g, consistent with the units they input and print it on screen? (if they input meters gravity would be 9.81 m/s^2, if they input inches gravity would be 384 in/s^2)
Walter Roberson on 14 Apr 2018
if iChoice == 0
g = 384;
else
g = 9.81;
end