473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Recursion Problem

Hi i'm currently trying to do a recursion homework assignment that does the factorial of any number the user inputs. I'm having trouble with getting the number the user inputs, and putting it in the function. Any suggestions?

#include <iostream>
using namespace std;

int fact(int n);

int main()
{
char ch;
int choice;

do
{
cout << "Pick any positive integer: " << fact(choice);
cin >> choice;


cout << "Press 'y' to run again, any other key to quit:";
cin.ignore();
cin.get(ch);
} while (ch =='y' || ch =='Y');

return 0;
}

int fact(int n)
{
int answer;

if (n==1)
return 1;
else
answer = fact(n-1) * n;
return answer;
}

this is what i have so far. and it says i have an "uninitialized local variable 'choice' used" in the bolded area.
May 1 '08 #1
3 891
Laharl
849 Expert 512MB
Right. Your current setup displays "Pick any positive integer." and then calculates the factorial of a number the user hasn't displayed yet! Move fact(choice) to a cout statement that's after the user has input the value and this will work fine.
May 1 '08 #2
Thanks! that fixed the problem. I couldn't even do that for something so easy and so simple. thats a beginner for you...
May 1 '08 #3
JosAH
11,448 Expert 8TB
Not that it matters much but change your if statement:

Expand|Select|Wrap|Line Numbers
  1. if (n == 1)
  2.  
to this:

Expand|Select|Wrap|Line Numbers
  1. if (n < 1)
  2.  
because those stupid users like to input silly negative numbers and you don't
want your stack to overflow.

kind regards,

Jos
May 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
2
by: Victor Lin | last post by:
Hi, I encounter a problem with pickle. I download a html from: ...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.