On Thu, Jun 4, 2009 at 3:24 PM, Olufowobi Lawal <[email protected]> wrote:
> So that left me wondering if recursion is in same category as "goto" , "know
> it, but don't use it".
There are quite valid places for goto, however while you're learning
you won't come across any of them.
> or are there some instance or some way where they can be used effeciently,
> like in my case?
>
> //the recursive type
> void ask_stuId(){
> string userInput;
> cout<<"Enter your full student Id"<<endl;
> getline(cin,userInput);
> if(isMatch_strFormat(userInput,"aaa/####/###"))
> stuId=userInput;
> else {
> cout<<"In correct format, ";
> ask_stuId();
Bad, and certainly not efficient compared to your loop version.
The user could just keep entering rubbish, and each time they do, some
more memory is taken up to store the next invocation of the function.
> }
> }
You should only use recursion where you have total control of the
input, and (by extension) where you can control the depth of
recursion.
--
PJH
http://shabbleland.myminicity.com/com
http://www.chavgangs.com/register.php?referer=9375