Hi I am a beginner and I wrote this program and everything works except when I input a letter instead of a number the program crashes any suggestions on how I can fix that and also any feedback on my program would be really appreciated.
Code://Programmer: Fernando B //Program: Factorial //Purpose: factor a number //need a boolean statement if the input is a letter?? //Formula 0! = 1 // n! = n * (n-1)! // Date: 12/21/10 #include <iostream> #include <string> #include <cmath> 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: "; cin >> n; if (n <= 0) { cout << "Try again, please enter a positive integer!!!\a" << endl; } else if ( n >= 1) { char inputCharacter = 0; cout << n << "! =" << factorial(n) << endl; cout << "Would you like to try a new number? (Y)es (N)o" << endl; cin >> inputCharacter; if ( inputCharacter == 'Y' || inputCharacter == 'y') { quit = false; } else if ( inputCharacter == 'N' || inputCharacter == 'n') { cout << "Quitting..."; quit = true; } } } return 0; system ("pause"); }



LinkBack URL
About LinkBacks


