Let's return to the problem that introduced this lesson and consider how to use
a while loop to enforce input constraints. Take a look at
``examples/kelvin.c''. You can also view it directly.
Study the program, run it a few times, and then try to answer the following
questions.
- Why do we set the value of temp to -1 before the loop?
- If the loop condition is initially true, what makes it eventually change
and become false so the loop can stop?
- What will happen if the user refuses to enter a positive number?
- How can we be confident that the final value of temp will be
positive?
Click here for the answers
Before we move along, try to write a function of no arguments that
prompts the user for and returns a positive temperature in degrees Kelvin. In
other words, package up the program that we just wrote as a function.
Click here for the answer
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah