If-Else Statement Exercise: Temperature Conversion
Create a C++ program called ex2_convert.cpp
with the following code in the Visual Studio project or copy the following code into another IDE:
This program demonstrates how to use if-else statement.
Test the program with these inputs:
C 100
F 32
c 0
f 0
d 0
F 32
c 0
f 0
d 0
Explain to yourself
- Why do the last three sets of the input values yield the same output?
- Why isn't c the same as C?
- Why would you type any number after d even though d is not a known conversion type?
Fix the problems
You should use two if-else
structures to fix the program:
- One will validate the letter as soon as it is typed, ie.
If the input letter is not C, c, F , or f, output "wrong letter".
then use an else to skip the rest of the program (do not ask for temperature, do not print result)
Do not return early.
- The other will choose what conversion to use.
Make sure to treat (C 100) and (c 100) the same,
and treat (F 32) and (f 32) the same;
- Use the same tests as before to confirm that you fixed the problems.
When you are done:
- Write comments that explain your if statements and boolean expressions. How they work?
- For this exercise, you will have:
ex2_convert.cpp
. - If you have completed everything, congratulations! Please upload your .cpp files and screenshots to URCourses.