So as it turns out I still had one more bug in the program when I asked to enter Y or N, the user could input any character and still go on... so here is the fix for that
Code:
/*Programmer: Fernando B
Program: Factorial
Purpose: factor a number
Formula 0! = 1
n! = n * (n-1)!
Date: 12/21/10
notes with long long I can only go as far as 65*/
#include <iostream>
#include <ios>
#include <cmath>
#include <limits>
using namespace std;
long long factorial (long long n)
{
if (n == 0) return 1;
return n * factorial (n-1);
}
int main ()
{
bool quit = false;
while (!quit)
{
long long n;
cout << "Enter a positive integer to compute the factorial of: ";
while (!( cin >> n )){
cin.clear(); // clear error state
cin.ignore(numeric_limits <streamsize>::max(), '\n'); // remove the unrecognized char
cout << "Invalid data input. Please try again: \a";
}
if (n <= 0)
{
cout << "Try again, enter a positive integer! \a" << endl;
}
else if ( n >= 1)
{
cout << n << "! =" << factorial(n) << endl;
}
{
bool quit1 = false;
while(!quit1)
{
char inputCharacter = 0;
cout << "Would you like to try a new number? (Y)es (N)o" << endl;
cin >> inputCharacter;
if (!( inputCharacter == 'Y' || inputCharacter == 'y' || inputCharacter == 'N' || inputCharacter == 'n'))
{
cout << "Enter Y or N, try again! \a" << endl;
quit1 = false;
}
else if ( inputCharacter == 'Y' || inputCharacter == 'y')
{
quit1 = true;
}
else if ( inputCharacter == 'N' || inputCharacter == 'n')
{
cout << "Quitting...";
quit1 = true;
quit = true;
}
}
}
}
return 0;
system ("pause");
}