How to make a loop of a calculator that keeps asking until the input is zero/nothing?

2 vues (au cours des 30 derniers jours)
Your solution must prompt user to input the option (DR or RD). If the option is DR, then ask for angle in degrees and the program displays the equivalent radians. If the option is RD then ask for angle in radians then the program displays the equivalent degrees. Your program should able to accept both scalar and matrix input. The script should keep running until no number is provided to convert. [The function isempty will be useful here]
i only got this far and there seems to be a problem with my code (when you type a number, it shows 2-4 answer)
choice = menu ('Choose the conversion you want to calculate','DR','RD','Other');
if choice == 1;
fprintf('You have choosen a Degree to Radian conversion');
fprintf('\n');
num = input ('Enter an angle in degrees: ', 's');
result = (num) * pi / 180;
no = str2double(result);
elseif choice == 2;
fprintf('You have choosen a Radian to Degree conversion');
fprintf('\n');
num = input ('Enter a number in Radian: ', 's');
result = (num) * 180 / pi;
no = str2double(result);
else
fprintf('Error mag\n');
end
fprintf('The result of the conversion is %d\n', no);

Réponse acceptée

Rik
Rik le 24 Nov 2020
You forgot to convert the result to a number, so it is a char array.
%radian = input ('Input a number: ', 's');
%Input a number: 1.2
% this result in:
radian='1.2';
radian=str2double(radian);
  6 commentaires
Ken L
Ken L le 2 Fév 2021
Hi Rik, since this question require isempty function, may I know where to insert the isempty function? Thank you.
Rik
Rik le 2 Fév 2021
What do you think? Your program should keep going until the user doesn't provide an input. How would you detect that?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by