//****************************************************************** // Activity program // This program inputs any number of temperature values and, for // each temperature, outputs an appropriate activity //****************************************************************** #include using namespace std; void GetTemp( int& ); void PrintActivity( int ); int main() { int temperature; // The outside temperature GetTemp(temperature); while (cin) // While not EOF... { PrintActivity(temperature); GetTemp(temperature); } return 0; } //****************************************************************** void GetTemp( /* out */ int& temp ) // This function prompts for a temperature to be entered, reads // the input value, checks to be sure it is in a valid temperature // range, and echo-prints it // Postcondition: // User has been prompted for a temperature value (temp) // && Error messages and additional prompts have been printed // in response to invalid data // && IF no valid data was encountered before EOF // Value of temp is undefined // ELSE // -50 <= temp <= 130 && temp has been printed { cout << "Enter the outside temperature (-50 through 130): "; cin >> temp; 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; } //****************************************************************** void PrintActivity( int temp ) // Given the value of temp, this function prints a message // indicating an appropriate activity { cout << "The recommended activity is "; if (temp > 85) cout << "swimming." << endl; else if (temp > 70) cout << "tennis." << endl; else if (temp > 32) cout << "golf." << endl; else if (temp > 0) cout << "skiing." << endl; else cout << "dancing." << endl; }