Reputation: 832
I am trying to write a simple program that Takes a Full Name entered by the user for Example "Peter Mills" and generates a Username From the First Letter of the First Name and the first 4 digits of the Last Name?
SO Output should be "pmill"
How on earth do i do this. Im still very new with Qt Please can someone shed some light on this for me. This is what i have at this stage
#include <QCoreApplication>
#include <iostream>
#include <QString>
#include<QStringList>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream cin(stdin);
QTextStream cout(stdout);
QString fullName;
cout << "Enter Your Full Name: " << endl;
cin >> fullName;
QStringList username = fullName.split(" ");
username[0]=username[0][1];
QString usernameJoin = username.join("");
cout << "Full name is : " << fullName << endl;
cout << "Username is : " << usernameJoin << endl;
return a.exec();
}
Upvotes: 0
Views: 193
Reputation: 7330
Here is an example based on your code that will do what you want :
QTextStream cin(stdin);
QTextStream cout(stdout);
QString firstName, lastName;
cout << "Enter Your Full Name: " << endl;
cin >> firstName >> lastName;
QString userName = (firstName.left(1)+lastName.left(4)).toLower();
cout << "Full name is : " << firstName << ' ' << lastName << endl;
cout << "Username is : " << userName << endl;
cin
stops at word boundaries, so you have to read the first and last names in two times.
Then you can use QString::left(int n)
to get the n leftmost characters of a QString, and QString::toLower()
to convert the result to lowercase.
This is exactly what we use above, we take the 1 first characters of the first name, the 4 first characters of the last name, put them together, and convert the result to lowercase.
Upvotes: 1