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.*/
#include <iostream>
usingnamespace 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;
}
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.
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.