Problem with program

Hi, I am just starting out with C++, and I was trying to write a program that gives you the factorial of a number. However, the program just gives 0!="Whatever number you typed".

Here is the code - can you help? Thanks.

#include <iostream>
using namespace std;

int main ()

{ int your_number;
int k=1;
int factorial;
int z=0;
int l=0;

cout<<"Please type a whole, positive number that isn't too big."<<endl;
cin>>your_number;

/*I also had a loop here that said while your_number<0, to type it again, but I don't think that was the problem.*/

factorial = your_number;

if(your_number=0)
{cout<<your_number<<"!=1"<<endl;}
else
{
while(your_number-k>0)
{
factorial=factorial*(your_number-k);
k=k+1;
}
}


cout<<your_number<<"!="<<factorial<<endl;

do(z=z);
while(l<10);

return 0;
}
Firstly, please use the code tags when posting code.

if(your_number=0)
Is the problem. You need two equal signs for a comparison.
Debugging is part of programming. If you don't have a debugger, you can insert trace statements. It turns out that the error is in this line:
 
if(your_number=0)
That should be comparison, not assignment. It's a common error.

Also, you need to be careful with statements like:
 
while(your_number-k>0)
The order of evaluation isn't always what you expect it to be and you can get suprising results. But in this instance, the code is correct.
http://msdn.microsoft.com/en-us/library/2bxt6kc4%28v=vs.71%29.aspx

I've added a trace statement to your code that demonstrates how you can add a trace statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

int main()
{
    cout << "Please type a whole, positive number that isn't too big." << endl;
    int your_number;
    cin >> your_number;

    // I also had a loop here that said while your_number<0,
    // to type it again, but I don't think that was the problem.
    int factorial;
    factorial = your_number;

    if (your_number == 0)  // I fixed this line
    {
        cout<<your_number<<"!=1"<<endl;
    }
    else
    {
        int k=1;
        while (your_number-k > 0)
        {
            cout << "factorial = " << factorial << " * (" << your_number << "-" << k << ")" << endl;

            factorial = factorial*(your_number-k);
            k = k+1;
        }
    }

    cout << your_number << "!=" << factorial << endl;

    int l=0;
    while (l<10);

    return 0;
}
Hi, thanks very much for both your replies.

kbw, does the line that you've added
(cout << "factorial = " << factorial << " * (" << your_number << "-" << k << ")" << endl;)
not just display a message? I'm not very sure what you mean by trace statements.

Thanks again.
It's called a trace statement in other contexts because it logs/traces the state of variables who's values are in question during the run of the program. But yes, it does just display a series of messages.

Once you're happy that the program's doing as you think, you can simply remove the statement.

Production operating systems often have logging services where applications can place diagnositic info for example syslog on Unix and eventvwr on Windows.
Last edited on
Topic archived. No new replies allowed.