/* testfunc08.cpp CIS 150 2008-06-10 David Klick This program demonstrates static local variables. */ #include using std::cin; using std::cout; void cont(); int counter1(); // function prototype int counter2(); // function prototype int main(int argc, char* argv[]) { int i; for (i=0; i<9; i++) { cout << counter1() << ", " << counter2() << '\n'; } cont(); return 0; } /* int counter1(); Arguments: none Returns: the value of its local count variable incremented by 10 (which will always be 10) Increment counter by 10 and return value. (non-static counter) */ int counter1() { int count = 0; count += 10; return count; } /* int counter2(); Arguments: none Returns: the value of its local static count variable incremented by 10 (which will increase each time this function is called) Increment counter by 10 and return value. (static counter) */ int counter2() { // static locals retain their value between function calls // the static initialization will only happen the first call static int count = 0; count += 10; return count; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: 10, 10 10, 20 10, 30 10, 40 10, 50 10, 60 10, 70 10, 80 10, 90 */