I need some help with a QStringList, please??



  • Here is my code right now and I was wondering how I could get the count of however many lines in that list... by the way I could do it with regular C++ but, I want to use Qt. As in QStringList.count() or something but.. I get a segment fault when I run my program after it encrypts the data. I know it has to be the count because, I remove the listUserInfo.count(), replace it with like 2 or something.. my program works perfectly. I don't understand why that would cause said, "Segment Fault". Any help would be appreciated thanks guys.

    [code] bool decryptUserFile(QString sFile)
    {
    // Open the usernames.dat file
    QFile inputFile(sFile);
    bool result = inputFile.open(QIODevice::ReadOnly);

        if (!result)
            return false;
    
        QTextStream in(&inputFile);
        QString fileContents = in.readAll();
    
        if (fileContents.endsWith(encMessage))
        {
            user_file_data = fileContents.replace(encMessage, "\0");
        }
    
        QString decData = calculateXor(user_file_data.toAscii(), theKey.toAscii());
    
        // rewrite the decrypted data to the same file
        QStringList listUserInfo = decData.split('\n');
    
        for (int i = 0; i < listUserInfo.count(); i++)
            sUserInfo[i] = listUserInfo[i];
    
        // close the inputFile
        inputFile.close();
    
        return true;
    }[/code]


  • @
    for (int i = 0; i < listUserInfo.count(); i++)
    sUserInfo[i] = listUserInfo[i];
    @

    Are you sure that: sUserInfo.size() >= listUserInfo.size() ??


    BTY: I can not understand what you are trying to do. If you are really doing "encryption or decryption.", you should not using QString/QTextStream/..., instead, you should always use QByteArray or char* .



  • I was wondering if anyone could tell me why this don't work and how to fix it please...

    [code] else if (!username.isEmpty() && !passcode.isEmpty())
    {
    for (int i = 0, j = 0; i < sUsername.count() && j < sPasscode.count(); i++, j++)
    {
    if ((username == sUsername[i]) && (passcode == sPasscode[j]))
    {
    // 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!"));
    }
    else
    {
    QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid username or passcode.\nTry again..."));
    }
    }
    }[/code]

    Thanks in advance...



  • You can easily figure out why your above code doesn't work when you debug your application.

    1. Set a breakpoint
    2. Step by Step

    BTW: why use two var: i & j ?



  • Hello
    I have several questions:

    Why didn't you create separate post for each of your problem?

    What should the last code block do? And what's the problem with it?

    What did you do to find out what's wrong?



  • The reason I didn't post in a new topic is because it's the same project I'm working on...
    The last code block should check the <QStringList>sUsername[i] == username && <QStringList>sPasscode[j] == passcode. And it doesn't do it.
    I just no what wrong and I used the debugger... (I know because, the whole thing worked perfectly before I added that in there.)

    I want to check them vars and if any username and password combination was entered then say something like, "You have successfully logged into this program."



  • Well, if i've understood you right, you want to check all combinations of login and password, i.e. n*m variants, where n is number of logins and m is number of passwords.
    If so, you're doing it in a wrong way, you should:
    @
    else if (!username.isEmpty() && !passcode.isEmpty())
    {
    for (int i = 0; i < sUsername.count(); j++)
    for (j = 0; j < sPasscode.count(); j++)
    {
    if ((username == sUsername[i]) && (passcode == sPasscode[j]))
    {
    // 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!"));
    }
    else
    {
    QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid username or passcode.\nTry again..."));
    }
    }
    }
    @
    But I don't understand the meaning of this procedure: if you want to authenticate user, then you should check that he entered correct password for some correct login. Something like:
    @
    else if (!username.isEmpty() && !passcode.isEmpty()) {
    if (users.count(username)) {
    if (passcodes[username] == passcode) {
    QMessageBox::information(this, tr("Login Screen"), tr("You have successfully logged on to this program!"));
    }
    else {
    QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid passcode.\nTry again..."));
    }
    }
    else {
    QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid username.\nTry again..."));
    }
    }
    @


Log in to reply
 

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