thanks
Printable View
thanks
I can actually answer someone elses question, if you want a loop, if you want it to be a true recursive call, this isn't it and I really CAN'T help you with recursion, really!
This code is in detail in the thread on prime numbers and stacks, but here is a shorter version:
as long as the user keeps answering the "do you want to go again" question with a Y or y (in this case), it will keep allowing use of the program. As soon as something (anything other than a Y or y) is entered the program terminates.Code:int main()
{
// variable declarations
//program banner - what the programs does and how
//ask the question - do you want to try your had at what the program does.
cin >> userResponse; //the users responds Y or y to continue, anything else to end
while (userResponse == 'Y' || userResponse == 'y')
{
//the meat of your program
//ask the question again.
cin >> userResponse;
}//end while
return 0;
}//end main
Hope this helps with the your problem.
Whether the user got the answer right or wrong, your functions should send control back to main and let main ask if they want to "do it again."
Generally, if something can be done easily with a loop, then do so. Recursion is an advanced technique that sucks up stack space everytime you use it.
. . . assuming that the compiler couldn't optimize the recursion into iteration. You'd be surprised at how often this happens.
That's not a good thing to depend upon, of course.
You're aware, of course, that these both do exactly the same thing?
Code:!(mathTest());
mathTest();
Just a tip, I see no point in this, besides some wasted code. It works, of course, but the much more efficient way would just be this:Code:for(int index=1; index<=2; index++){
if (index == 1)
{
random_integerA = (rand()%10);
}
else
{
random_integerB = (rand()%10);
}
}
Also, maybe consider better indentation? (not horrible, but could be better)Code:random_integerA = (rand()%10);
random_integerB = (rand()%10);
Please read the following entry of the FAQ:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
whilst dwks makes a good point, you can't actually rely on recursion being optimized to a loop - you can examine the code and see if it's done that, but you can't rely on it. A typical example where it doesn't happen is if you use any of the input variables after the recursion call.
Using explicit loops is the way to go. Recursion should only be used where the depth of the recursion can be determined. Using recursion for "boundless" repetitions will lead to a stack overflow eventually [ok, not likely in this case, as the user will probably get bored before it's got anywhere near filling the stack, but that's not a good reason to use that method].
--
Mats
OP: Please don't delete your query after having been answered. Doing so makes it impossible for other people to find your problem and the solution in the forum, thus forcing them to ask the same thing again when it could have been answered by a forum search.