I am working on homework. I am to prompt user for temperature in Celsius and then ask them to choose if they want it converted to F or C. I have this so far. It promted user for temp in C and converted to F before I started to add a while loop,

27 views (last 30 days)
I am working on homework. I am to prompt user for temperature in Celsius and then ask them to choose if they want it converted to Farenheit or Kelvin. I have this so far. It promted user for temp in C and converted to F before I started to add a while loop, which is how I was thinking to get the user to choose the input, I also made the display a comment while I play with it. My question really is how to write ask user to choose Farenheit or Kelvin for the answer. I think maybe ifelse would work better. Also I did try to get with my professor about this he is not available before this is due. Just need some help.
Thanks yall
%This will take temp in Celsius and return it in either Farenhiet or Kelvin
C = input('Please enter a temperature value in Celsius.');
K = C+273.15
F = (9/5)*C+32
while 1
end
%fprintf('The temperature in Farenheit is: %d.\n', F)

Accepted Answer

Jess Lovering
Jess Lovering on 5 Nov 2019
Edited: Jess Lovering on 5 Nov 2019
You could use the question dialogue box, see the example below. You could first prompt for the temperature number and then ask if they want Farenhiet or Kelvin with the questdlg function similar to the example below. Instead of the "desert =" lines you would substitue in your code for the right conversion formula. You will only need 2 cases for your assignment. Hope this helps get your started in the right direction.
answer = questdlg('Would you like a dessert?', ...
'Dessert Menu', ...
'Ice cream','Cake','No thank you','No thank you');
% Handle response
switch answer
case 'Ice cream'
disp([answer ' coming right up.'])
dessert = 1;
case 'Cake'
disp([answer ' coming right up.'])
dessert = 2;
case 'No thank you'
disp('I''ll bring you your check.')
dessert = 0;
end
  4 Comments
Adam Juckniewitz
Adam Juckniewitz on 6 Nov 2019
Just wanted to say thanks agian! Here's how it turned out.
It's probably cluttered, I put the variables in twice to be sure. But still in progress. Thankful for this community.
C = input('Please enter temperature in Celsius');
KorF = input('Would you like to convert to K or F?','s')
F = (9/5)*C+32
K = C+273.15
if KorF == 'K'
K = C+273.15 %This converts Kelvin to Celsius
disp(['The temperature in Kelvin is: ',num2str(K),'.'])
elseif KorF == 'F'
F = (9/5)*C+32
disp(['The temperature in Farenheit is: ',num2str(F),' '.' ])
end
Jess Lovering
Jess Lovering on 7 Nov 2019
Looks great! I have gotten so much use out of MATLAB answers that other people have asked so I have started to try to answer some myself when I get the chance. I tested it out and it worked well for me. It does print a lot to the screen when you run it, so you can suppress that with a semicolon at the end of those lines if you want it to only display the lines based on the user selection:
C = input('Please enter temperature in Celsius');
KorF = input('Would you like to convert to K or F?','s');
F = (9/5)*C+32;
K = C+273.15;
if KorF == 'K'
K = C+273.15; %This converts Kelvin to Celsius
disp(['The temperature in Kelvin is: ',num2str(K),'.'])
elseif KorF == 'F'
F = (9/5)*C+32;
disp(['The temperature in Farenheit is: ',num2str(F),' '.' ])
end

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!