factor program

This is a discussion on factor program within the C++ Programming forums, part of the General Programming Boards category; I need some help with this program, it is homework but I can't figure it out. This is what I ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    6

    factor program

    I need some help with this program, it is homework but I can't figure it out. This is what I have to do:

    Modify the FACTOR program in this chapter so that it repeatedly asks for a number and calculates its factorial, until
    the user enters 0, at which point it terminates. You can enclose the relevent statements in FACTOR in a while loop or a
    do loop to achieve this effect.

    Here is what I have thus far:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	unsigned int numb;
    	unsigned long fact=1;	//long for larger numbers
    
    	cout << "Enter a number: ";
    	cin >> numb;	//get number
    	for(int j=numb; j>0; j--)	//multiply 1 by
    		fact *= j;				//numb, numb-1, ..., 2, 1
    	cout << "Factorial is " << fact << endl;
    	int dummy;
    	cin >> dummy;
    	return 0;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    That just looks like the unmodified factorial program that's in the book... You're supposed to wrap it in a loop that breaks when the user enters '0'. Did you learn about loops, yet?

    ...and get those "dummy" statements out of there and just replace it with a
    Code:
    cin.ignore();
    cin.get();
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    yeah I'm just learning loops. It is from what is in the book but I can't figure out how to change it to what it is asking for. Can you give me a hint or something?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    I gave you a hint... use a loop.

    Here is a few more...

    Read your book.
    Ask your teacher.
    ...and if you're still expecting code, then perhaps you should read the Homework Policy.

    All you need is a loop. It's literally two lines of code and one of them is a closing brace. If you want to work around your factorial calculation and output on zero then it's 3-4 lines of code tops. Which means, sans studying loops (which by the way... you WILL be using if you plan on continuing programming past this assignment) this is at a maximum a 60 second assignment. So read your book, learn loops, and just do it.
    Last edited by SlyMaelstrom; 09-26-2006 at 10:20 PM.
    Sent from my iPadŽ

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by SlyMaelstrom
    That just looks like the unmodified factorial program that's in the book... You're supposed to wrap it in a loop that breaks when the user enters '0'. Did you learn about loops, yet?

    ...and get those "dummy" statements out of there and just replace it with a
    Code:
    cin.ignore();
    cin.get();
    Why don't you like << and >> operators?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Why don't you like << and >> operators?
    Formatted input in not necessary here, and cin.ignore()/cin.get() is somewhat more canonical.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21