[Solved] Reading characters from QInputDialog and concatenating/append
-
So I am having 2 issues with my code:
The one is displaying the username and password in the QMessagebox and the other is generating the random password from the appended string.
I am sure it is some small issues but I am not picking it up. I am using QT 5.3.0
Here is my code:
@// Declarations of variables
bool ok;
QString fullName;
QString userName;
QString passWord;
QString genPass;fullName = QInputDialog::getText(0,"Username","Please Enter Name and Surname:", QLineEdit::Normal," ", &ok); if (ok && !fullName.isEmpty()) { QStringList elements = fullName.split(" "); userName = elements[0].left(4) + elements[1].left(1); } //If username is less than five characters while (userName.length() < 5) { userName += '0'; } //Remove all whitespaces from string userName = userName.simplified(); userName.replace( " ", "" ); //First letter of username to uppercase userName = " "; userName.replace(0, 1, userName[0].toUpper()); passWord = userName; //For loop to generate random password of 5 characters from concatenated name/surname string for (int i = 0; i < 5; i++){ passWord[i] = genPass [qrand() % (sizeof (genPass) -1)]; do{ passWord[i] = genPass [qrand() % (sizeof (genPass) -1)]; }while (passWord[i] = ' '); } //QMessageBox to display username and password QString response = QString(" Username: %1 \n Password: %2") .arg(userName).arg(passWord); QMessageBox::question(0, "Username and Password", response, QMessageBox::Ok);
return EXIT_SUCCESS;
}
@ -
Hi SGalst, is there a better way to do the password generator.
-
You can inspiration from e.g. "here":http://www.cplusplus.com/forum/windows/88843/
What issues are you facing ?
-
Just 2 issues:
-
The QMessageBox is not displaying the concatenated username or the password.
-
My randomizer is not generating random 5 character passwords from the concatenated first name and surname.
I will look at the link as well.
Thanks
-
-
What are you currently getting in your message box ?
-
Nothing at all. It just pops up when I press OK and shows the username: and password: but with no data.
-
@ //First letter of username to uppercase
userName = " "; << You're a replacing userName content with a space
userName.replace(0, 1, userName[0].toUpper());
@ -
Thanks, that fixed the username display issue.
When I enter the fullname of john black , I get Johnb instead of Blacj
-
Like I said, using only one input your user can put their information in the order they want so first name/last name or last name/first name. You really should write a little custom dialog that uses two QLineEdit for that. It will be clearer for your users
-
SGalst thanks for all the assistance. I have solved the issues.