Thread: Factorial program problem

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Factorial program problem

    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");
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    cin >> is formatted input. If the input doesn't match the format, then >> is the wrong thing to do. Look at the FAQ which exactly matches your question "Why does my program enter an infinite loop if the user inputs invalid data? (C++) "

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    cin >> is formatted input. If the input doesn't match the format, then >> is the wrong thing to do. Look at the FAQ which exactly matches your question "Why does my program enter an infinite loop if the user inputs invalid data? (C++) "
    Thanks a lot tabstop I will be spending some time at the FAQ from now on, so here is my finished program...
    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 <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)
    
        {
            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");
    
    
    }

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    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");
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  3. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM