How to restrict the number of attempts in a login form ?
-
hello everybody. i am making a login form. in that i have lineedit & lineedit_2 that take login id & password respectively. i want to restrict the maximum number of login attepts to three. also i want to display the message about the number of remaining attempts in label_3. i want that the checking starts the user hits a pushbutton. also to make sure that the changes to the value of loop counter "int i" persist between different function calls, i have used pass by reference. i have tried this code but it is becoming an infinite loop.
@
void D1::on_pushButton_clicked()
{
i= setval(&i);
while(i<3)
{
if(!(ui->lineEdit->text().isEmpty()) && (!ui->lineEdit_2->text().isEmpty()))
{
ui->lineEdit_3->setText(QString::number(i));
if((ui->lineEdit->text()=="admin") && (ui->lineEdit_2->text()=="admin"))
{
D2 d;
d.exec();
}
else
{
ui->label_3->setText("Unsuccessful Attempt");
ui->lineEdit->clear();
ui->lineEdit_2->clear();
QString x= QString("the number of attempts left is %1").arg(i);
i=chechval(&i);
ui->label_3->setText(x);
}
}
else
{
if(i==2)
{
ui->label_3->setText("Max number of attempts finished");
}
else
{
QString x= QString("the number of attempts left is %1").arg(i);
i=chechval(&i);
ui->label_3->setText(x);
}
}
}
}int D1::setval(int *ptr)
{
*ptr= 0;
return(*ptr);
}
int D1::chechval(int *ptr)
{
*ptr= *ptr+1;
return(*ptr);
}@
please tell me how can i improve upon this. -
Please use "code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 in your post. This shall make the code sections readable.
-
hello koahnig. i have modified the post as told by you. i must admit that the code looks really nice & readable now.
-
Hmm, if I make a suggestion is to not have the D1 (I presume a dialog) start a new dialog (D2.exec), but rather have the D1 be started by the parent, wait for it to be closed or accepted, check the codes entered or maybe use the default return codes if the entry was valid and go and execute the D2 dialog, or go straight past it.
@void D1::D1()
{
iTries = 1;
ui->lineEdit->clear();
ui->lineEdit_2->clear();
ui->label_3->setText(QString("Attempt %1 of 3").arg(QString::number(iTries)));
}void D1::on_pushButton_clicked()
{
// Check if log in was correct this time
if ( (!ui->lineEdit->text().isEmpty()) &&
(!ui->lineEdit_2->text().isEmpty()))
{
if((ui->lineEdit->text()=="admin") && (ui->lineEdit_2->text()=="admin"))
{
accept(); // this will close the dialog with an accept return code!
}
}// Check if the user did it too many times iTries++; if (iTries > 3) { QMessageBox::information(this, "Log in Failed!", "Number of attempts reached"); reject(); // This will close the dialog and give a reject return code! } // setup the Gui for next login text ui->lineEdit->clear(); ui->lineEdit_2->clear(); ui->label_3->setText(QString("Attempt %1 of 3").arg(QString::number(iTries)));
}
// parent code: D1 * D1Dialog = new D1; if (D1Dialog->exec() == QDialog::Accepted) { D2 * D2Dialog = new D2; D2->exec(); }
@
It is also better practice to use the class pointer <name> = new class; for dialogs etc. This has to do with stack and heap stuff in the background. Don't be afraid to use the pointers. Qt is very great in code editor to replace a used . into a -> for members and to destroy the allocated data when the dialog of his parent is destroyed.
Good luck with it.
(btw, I didn't test the code, so no idea if it is perfect already)