thanks
thanks
Last edited by nicz888; 11-26-2007 at 09:12 AM. Reason: i got it
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."
Last edited by clegs; 11-25-2007 at 05:30 PM.
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();
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law