/* CS401 - Intro to CompSci code from week 6 cut and paste this into your compiler's editor if you want to run any of the programs. */ /*************************************************************************/ // Bob & Doug McKenzie #include #include using namespace std; int BDtoMetric(int); // prototype int main() { int valToConvert, answer; cout << "Gimmee an integer, you hoser: "; cin >> valToConvert; // call the Bob & Doug function answer = BDtoMetric(valToConvert); // display the result cout << valToConvert << " is " << answer << " in metric.\n"; return 0; } // definition of BDtoMetric() - "double it and add 30" int BDtoMetric(int x) { return (2*x + 30); } /* alternate (longer) definition int BDtoMetric(int x) { int result; result = 2*x + 30; return result; } */ /*************************************************************************/ // a parameterless, void function #include #include using namespace std; void myFunc(); int main() { cout << "I'm in main()\n"; myFunc(); // transfers control, void so no return value here cout << "Back in main()\n"; } // definition of (the mostly useless) myFunc() void myFunc() { cout << "I'm in myFunc()!\n"; return; // not necessary } /*************************************************************************/ // uses a boolean function, used to verify relative humidity input #include #include using namespace std; bool acceptableRH(double); int main() { double relHum; // // other code // cout << "Please enter the relative humidity (between 0.0 and 1.0): "; cin >> relHum; // verify it - loop while it is bad input while (! acceptableRH(relHum)) { cout << "Out of range. Please try again: "; cin >> relHum; } // // other code // } // end main() // definition of acceptableRH() - it returns a boolean bool acceptableRH(double d) { return ((0.0 <= d) && (d <= 1.0)); } /* alternate definition - longer, but might make better sense to you bool acceptableRH(double d) { bool result; result = (0.0 <= d) && (d <= 1.0); return result; } */ /*************************************************************************/ // same program, but with more robust range checking. #include #include using namespace std; bool withinRange(double, double, double); int main() { double relHum; // // other code // cout << "Please enter the relative humidity (between 0.0 and 1.0): "; cin >> relHum; // verify it - loop while it is bad input while (! withinRange(relHum, 0.0, 1.0)) { cout << "Out of range. Please try again: "; cin >> relHum; } // // other code // } // end main() // definition of acceptableRH() - it returns a boolean bool acceptableRH(double d, double low, double high) { return ((low <= d) && (d <= high)); } /*************************************************************************/