Solving CuTee's Factorial Problem with QValidator.h

  • Thread starter Thread starter Tim1955
  • Start date Start date
  • Tags Tags
    Factorial
AI Thread Summary
The discussion revolves around a programming issue related to calculating factorials in a Qt application. The code snippet provided initializes a QDoubleValidator for input validation and attempts to compute factorials recursively. However, the program encounters problems when an unfactorable number is input, causing it to run uncontrollably. Suggestions for resolving this issue include implementing error handling to validate input before performing calculations and considering the use of a QIntValidator instead of a QDoubleValidator to restrict inputs to integers, which are appropriate for factorial calculations. The importance of ensuring proper input format and error management is emphasized to enhance program stability and functionality.
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
 
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top