|
|
|
|
|
while (cin && // While not EOF and
(temp < -50 || temp > 130)) // temp is invalid
{
cout < Temperature must be
< -50 through 130. < endl;
cout < Enter the outside temperature: ;
cin >> temp;
}
if (cin) // If not EOF
cout < The current temperature is
< temp < endl;
} |
|
|
|
|
|
|
|
|
|
Unfortunately, if we make this improvement, the main function will be stuck in an infinite loop because GetTemp won't let us enter the sentinel value-1000. If the original implementation of GetTemp had been physically hidden, we would not have relied on its unusual feature of not performing error checking. Instead, we would have written the main function in a way that is unaffected by the improvement to GetTemp: |
|
|
|
|
|
|
|
|
|
int main()
{
int temperature;
GetTemp(temperature);
while (cin) // While not EOF
{
PrintActivity(temperature);
GetTemp(temperature);
}
return 0;
} |
|
|
|
|
|
|
|
|
|
Later in the book, you learn how to write multifile programs and hide implementations physically. In the meantime, conscientiously avoid writing code that depends on the internal workings of a function. |
|
|
|
|