Hello, i'm a newbie with c++.. all help and explanation are welcome..my problem is, i try to make a factorial program in c++ . Here is my code:

#include <iostream>
using namespace std;

int main()
{
    int a,b,fact=1;
    char key;
do
{    
    cout<<"\nPlease enter a positive integer : ";
    cin>>b;
   
    for (a=b; a>=1; a--)
    fact=fact*a;
    
    cout<<"The output of "<<b<<"! is : "<<fact<<endl;

    cout<<"\nPlease enter y to continue : ";
    cin>>key;
}
while ( key == 'y' || key == 'Y');

 
  system("pause");
  return 0;
}

to make a single factorial calculation is easy for me. but i want to make it that just put any number and it will give the answer every time the program is repeated . For example, when i run my code in c++ it wil give me this:

Please enter a positive integer : 5
The output of 5! is : 120

Please press y to continue :

Now, when i enter y, it will goes like this:


Please enter a positive integer : 5
The output of 5! is : 120

Please press y to continue : y

Please enter a positive integer :

So, i decided to put 6, but the problem is 6! suppose to be 720.. but this program gave me 6! is 86400????

the ouput is like this:



Please enter a positive integer : 5
The output of 5! is : 120

Please press y to continue : y

Please enter a positive integer : 6
The ouput of 6! is : 86400

Please press y to continue :

It goes like that whenever i enter new value. It's like it multiply the first factorial value with the variable a;.. need your opinion and please don't give me direct answer..i need to learn!!

Recommended Answers

All 8 Replies

You need to re-initialize fact to 1 when the loop restarts. You are getting such an unusual answer because you haven't reset your program's state. You must always reset your program's state if you are implementing a "would you like to do again?" style loop in it.

dear i have solution but i want u to learn its very easy.....
you should clear the value of 'fact' after displaying it own screen.
clear mean initalize it by one again

You need to re-initialize fact to 1 when the loop restarts. You are getting such an unusual answer because you haven't reset your program's state. You must always reset your program's state if you are implementing a "would you like to do again?" style loop in it.

dear i have solution but i want u to learn its very easy.....
you should clear the value of 'fact' after displaying it own screen.
clear mean initalize it by one again

what do you guys mean by reinitialize the 'fact'?
do i have to declare the fact value as in fact=1 again after the for statement?

got it !!! thanks guys for pointing out...now i've learned something new

cin.clear();
cin.ignore(fact=1);

the clear function and ignore function
am i correct so far?? here's the new code point me out if there's anymore mistake

#include <iostream>
using namespace std;

int main()
{
    int a,b,fact=1;
    char key;
do
{    
    cout<<"\nPlease enter a positive integer : ";
    cin>>b;
   
    for (a=b; a>=1; a--)
    fact=fact*a;
    cout<<"The output of "<<b<<"! is : "<<fact<<endl;
    cin.clear();
    cin.ignore(fact=1);
    
    cout<<"\nPlease enter y to continue : ";
    cin>>key;
       
}
while ( key == 'y' || key == 'Y');

 
  system("pause");
  return 0;
}

got it !!! thanks guys for pointing out...now i've learned something new

cin.clear();
cin.ignore(fact=1);

the clear function and ignore function
am i correct so far?? here's the new code point me out if there's anymore mistake

#include <iostream>
using namespace std;

int main()
{
    int a,b,fact=1;
    char key;
do
{    
    cout<<"\nPlease enter a positive integer : ";
    cin>>b;
   
    for (a=b; a>=1; a--)
    fact=fact*a;
    cout<<"The output of "<<b<<"! is : "<<fact<<endl;
    cin.clear();
    cin.ignore(fact=1);
    
    cout<<"\nPlease enter y to continue : ";
    cin>>key;
       
}
while ( key == 'y' || key == 'Y');

 
  system("pause");
  return 0;
}

Definitely not! That's not what they're for. They're for manipulating the input and output streams. They do absolutely nothing to the value of any variable. Did you even do test run on that? If you had, you'd already know that doesn't work.

>>do i have to declare the fact value as in fact=1 again after the for statement?
Not after the for, after the do. What we're saying is that you should move your declaration/initialization of fact from Line 6 to between Lines 9 and 10.

Now i understand what do you mean, i have to declare the variable after the DO statement right? so it should look something like this right :

int main()
{

    //int a,b,fact=1;<--not using this here anymore 
    char key;
    
 
do
{    
    int a,b,fact=1; // declare variable here right?
    cout<<"\nPlease enter a positive integer : ";
    cin>>b;
    
    for (a=b; a>=1; a--)
    fact=fact*a;
   
    if (b<0)
    cout<<"\n\nTHAT IS AN INVALID POSITIVE NUMBER!!!\n"<<endl;

    else    
    cout<<"The output of "<<b<<"! is : "<<fact<<endl;
   // cin.clear();
   // cin.ignore(fact=1);
    
    cout<<"\nPlease enter y to continue : ";
    cin>>key;
       
}
while ( key == 'y' || key == 'Y');

 
  system("pause");
  return 0;
}

so my question is, if i want to make the "would you like to do again?" style loop in it. i have to declare the variable after the loop right?

Partiallly correct, you need to re-initialize the relevant variables inside the loop. They don't necessarily have to be declared within the loop, but they need to be initialized. Although they are frequently performed together, there is a difference between the two and they need not occur at the same time. Something like this would be okay too:

int main()
{

  int a,b,fact;   //variable DECLARATIONS
  char key;
    
 
  do
  {    
    fact = 1;     //variable INITIALIZATIONS
    cout<<"\nPlease enter a positive integer : ";
    cin>>b;
    
    /* ... other statements ... */
       
  }
  while ( key == 'y' || key == 'Y');

  /* ... other statements ... */

  return 0;
}

Also, I noticed that your input validation is in the wrong spot. You really should validate your input before you try to do anything with it. Your for loop should be part of your if statement's else section.

now i've got it...thnx mate..i'm learning something...=)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.