Im trying to make a program that generates all the verses of the song "The Twelve Days of Christmas" using recursion (its homework). Ive read what the book says several times, researched online, etc etc. Im not looking for anyone to do the homework for me...I just need some guidance. Im really lost on this and any help would be appreciated.

I cant use for loops or while loops.
I can only have lines from each verse appear only once in the code.

Recommended Answers

All 5 Replies

In recursion you need a "base case". Here I guess the base case is the first line of the song. So your function should probably be called PrintAllPreviousVerses(int verse) and just call it recursively decrementing 'verse' by 1. This actually doesn't seem like an ideal case to teach recursion, but maybe I just don't know what I'm talking about :)

OK, this might be considered cheating and would probably be the worst-basic-solution, but here goes nothing:

Can't you store the verse's in a 2-d array, word-wise then increment index using recursion?

char arr[][50]={"Word1","Word2"...};

I'd use a std::vector<std::string> no matter how you plan to do the recursion.

I'd use a std::vector<std::string> no matter how you plan to do the recursion.

I'd echo on that, it will make your problem simpler.

Thanks for all of your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.