I'm having a problem with a loop not sure whats going on... please help.



  • Here is the code... there must be something wrong because what it doesn't crash to program at all but, it doesn't check each individual string in the usernames.dat and passcodes.dat files. I've been working on this project for 7 and a half days now. Can't figure out why this don't work, maybe a fresh pair of eyes will figure out where I messed up. Thanks in advance for any help.

    The Code:
    [code]void SimpleLoginGui::on_loginButton_clicked()
    {
    // initialize the username and passcode variables
    QString username = ui->nameEdit->text();
    QString passcode = ui->codeEdit->text();

    QByteArray usernameArray = username.toAscii();
    QByteArray passcodeArray = passcode.toAscii();
    QByteArray sUsernameArray, sPasscodeArray;
    
    size_t numStrings1 = sUsername.count();
    size_t numStrings2 = sPasscode.count();
    
    QString username2, passcode2, testString1, testString2;
    
    while (true)
    {
        for (int i = 0; i < numStrings1; i++)
        {
            for (int j = 0; j < numStrings2; j++)
            {
                testString1 = sUsername[i];
                testString2 = sPasscode[j];
    
                sUsernameArray = testString1.toAscii();
                sPasscodeArray = testString2.toAscii();
    
                if ((strlen(testString1.toAscii()) > 0) || (strlen(testString2.toAscii()) > 0))
                {
                    for (int a = 0; a < strlen(testString1.toAscii()); a++)
                    {
                        usernameArray[i] == sUsernameArray[i];
                    }
    
                    username2 = usernameArray;
    
                    for (int b = 0; b < strlen(testString2.toAscii()); b++)
                    {
                        passcodeArray[j] == sPasscodeArray[j];
                    }
    
                    passcode2 = passcodeArray;
                }
            }
        }
    
        if (username == "" && passcode == "")
        {
            // Enter something
            QMessageBox::information(this, tr("Login Screen"), tr("You must enter something in both of the text boxes!"));
        }
        else if (username == "" || passcode == "")
        {
            // Enter something
            QMessageBox::information(this, tr("Login Screen"), tr("Username or passcode field empty."));
        }
        else if (!username.isEmpty() && !passcode.isEmpty())
        {
            if ((username == username2) && (passcode == passcode2))
            {
                // You are successfully logged on to the program
                ui->nameEdit->clear();
                ui->codeEdit->clear();
                QMessageBox::information(this, tr("Login Screen"), tr("You have successfully logged on to this program!"));
                break;
            }
            else
            {
                QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid username or passcode.\nTry again..."));
                break;
            }
        }
    }
    

    }[/code]


  • Moderators

    The two for loops from line 30 to 40 are really obscure.
    Line 32 and line 39 just do a comparison on equality, but no assignment, if that is your intention.
    Furthermore, why are you having a loop around those statements?



  • Yes, I could not figure out what he want to achieve too.

    Seems all he trying to implement is something like is:

    @
    void SimpleLoginGui::on_loginButton_clicked()
    {
    // initialize the username and passcode variables
    QString username = ui->nameEdit->text();
    QString passcode = ui->codeEdit->text();

    if (sUsername.contains(username) && sPasscode.contains(passcode)) {
        //right
    } else {
        //wrong
    }
    

    }
    @



  • That's exactly what I'm trying to achieve... I just haven't used qt for that long just downloaded it about 2 weeks ago. Just to try it and see if I like. It does make programming a lot easier. Thanks for the quick replies guys. Yes since there is no need for the loop. I'll remove it, but I was trying to mix c++ and qt together. So it made my code a lot harder to understand. I guess.



  • [quote]if (sUsername.contains(username) && sPasscode.contains(passcode))[/quote]

    Sure that contains() is correct here?

    If sUsername and sPasscode are QStringList's, then you check whether the username-list contains the username and the passcode-list contains the passcode. But not whether the given passcode actually matches the username. So somebody could login with user A's name and user B's passcode!

    And if sUsername and sPasscode are QString's, then with contains() somebody could login with any username/passcode that is a substring of the correct username/passcode. Actually it would be enough to guess one character that appears somewhere within the username or passcode...



  • [quote author="MuldeR" date="1336253569"]

    Sure that contains() is correct here?
    [/quote]
    ;-) Yes, I don't think this logic is reasonable.

    As I said above, I could not figure out what he want to achieve. but all he trying to implement is that.


    And we know that, If he using a QMap instead of two QList s, Code will be more reasonable and shorter.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.