Solving CuTee's Factorial Problem with QValidator.h

  • Thread starter Thread starter Tim1955
  • Start date Start date
  • Tags Tags
    Factorial
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
Tim1955
Messages
1
Reaction score
0
PHP:
#include<qvalidator.h>
//int input;

void Factor::init(){
    InputEdit->setValidator(new QDoubleValidator(InputEdit));

    FactorNumber();
    InputEdit->selectAll();
}

int Factor::factorials(int n){
    if(n<=1) return 1;
    FactorialDisplay->setText(QString::number(n*factorials(n-1),1));
    return n*factorials(n-1);
}

void Factor::FactorNumber()
{
    int input=InputEdit->text().toDouble();
    factorials(input);   
}

*** 2 edit boxes: InputEdit for input number; FactorialDisplay to display factor "in action"
*** 1 button called FactorButton implements factorNumber() connecting to the main form named Factor.

No errors but program run wild when unfactorable number is used and my text edit box does display factoring process till input equals 1

Help me with this little problme please.
Thanks
 
Physics news on Phys.org
for reaching out for help! Based on the code provided, it looks like you are trying to create a program that calculates factorials. A factorial is a mathematical operation that multiplies a given number by all the numbers below it. For example, the factorial of 5 would be 5 * 4 * 3 * 2 * 1 = 120.

To address the issue of the program running wild when an unfactorable number is used, I would recommend adding some error handling to your code. This can be done by using conditional statements to check if the input is a valid number before proceeding with the factorial calculation. You could also consider using a try/catch block to catch any potential errors and handle them appropriately.

Additionally, it looks like you are using a QDoubleValidator to restrict the input to only accept double values. This is a good practice to ensure that the input is in the correct format before performing calculations. However, you may also want to consider using a QIntValidator if you only want to accept integer values for factorials.

I hope this helps with your problem! Keep up the good work on your program.