/* Recursion: a recursively-defined song about recursion (would anything else be appropriate?) J.P. Dougherty Spring 2001 */ // standard IO and string #include #include using std::cin; using std::cout; using std::endl; using std::string; const int base = 0, obnoxious = 4; int GetCount() { int count; cout << endl << "How many verses would you like to see?: "; cin >> count; if ((base <= count) && (count < obnoxious)) return count; // OK else return GetCount(); // try again } string Verses(int i) { string lyrics = ""; if (i == 0) return lyrics; else { lyrics += "Recursion, recursion,\n"; lyrics += "We'll sing about recursion,\n"; if (i == 1) { lyrics += "Now the case has reached its base\n"; lyrics += "Ending the recursion.\n\n"; return lyrics; } else { lyrics += "Until the case has reached its base\n"; lyrics += "We'll sing about ....\n\n"; return lyrics + Verses(i-1); } } } // driver functions int main() { int numVerses = GetCount(); cout << endl << "Recursion" << endl << endl; cout << "J.P. Dougherty c2003" << endl << endl; cout << Verses(numVerses) << endl; return 0; }