Hey guys, theres a problem with the loop inside the yesno function here.. it kinda of hmm.. keeps the first number i enter and gives me the factorial of that number even if i enter another number the second time.. also my no option still gives me the loop even though I put a return 0; (?)
I will really appreciate any help I can get.. I've been struggling with this and its due tomorrow at noon >.<
Thanks in advance.
#include <iostream>
using namespace std;
int yesno();
double factorial(double num); //factorial prototype
int main ()
{
int number;
char choice;
cout << "Please enter a number: ";
cin >> number;
while(number > 0)
{
int x = factorial(number);
cout << "Factorial = " <<x <<endl;
cout << "Would you like to go again?";
cin >> choice;
if (choice == 'n' || choice == 'N')
{ cout <<"Thank you for using this program." << "\n";}
else if (choice == 'y' || choice == 'Y')
{
char choice = yesno();
}
else {cout << "Pay attention and start again" <<"\n";}
}
return 0;
system ("pause");
}
//yesno
int yesno()
{
do{
int number;
char choice;
cout << "Please enter a number: ";
cin >> number;
while(number > 0)
{
int x = factorial(number);
cout << "Factorial = " <<x <<endl;
cout << "Would you like to go again?";
cin >> choice;
if (choice == 'n' || choice == 'N')
{
cout <<"Thank you for using this program." << "\n";
return 0;
}
else if (choice == 'y' || choice == 'Y')
{
char choice = yesno();
return 1;
}
else
{
cout << "Pay attention and start again" <<"\n";
}
}
} while (yesno() == 1);
}
//compute factorial
double factorial (double num)
{
int fac=1;
for(int m=1; m<=num; num--)
fac = fac * num;
return fac;
}