generating Username

Oct 23, 2020 at 5:57pm
I am new here. Any idea how I can get this function in c++?

Function 1
a.) Member function name: generateUsername
b.) Parameters: 2 strings. First parameter is first name and the second represents last name respectively
c.) Return Type: string
d.) Purpose: should return the first letter of the person’s first name combined with their last name. So, a person who has a first name, Joe and last name, Crow, will get the username JCrow.
Last edited on Oct 23, 2020 at 6:13pm
Oct 23, 2020 at 7:23pm
the string + operator works with individual letters.
you can say
gen = fname[0] + lname;
to do this -- just wrap that up in a function as per your book/notes on functions. Give it a try... post your effort if you get stuck again.
Oct 26, 2020 at 4:48am
You need to store your data first. Read it from file or make it const string. Make your Input function.

For Function1
char*/*or string */ GenerateUserName(char& szFName, char& szLName); // string.c_str();
{
// ...
string firstName = szFName;
firstName = firstName.substr(0, 1);
firstName.append(szLName);
// ...
return firstName.c_str(); // return firstName; out of region, not allowed.
}
Oct 26, 2020 at 11:56am
The requirement is to return a std::string. As per jonnin:

1
2
3
4
5
6
7
#include <string>

std::string generateUsername(const std::string& first, const std::string& last)
{
	return first[0] + last;
}

Oct 26, 2020 at 3:25pm
Thanks for the help.
would this be correct?

Converter::Converter()
{
Converter();
}
string Converter::generateUsername(string fname, string lname)
{
string username;
fname = fname.substr( 0, 1); // get the first letter in fname

username = fname + lname; // joing fname and lname string to form username

return username;

}
Oct 26, 2020 at 3:54pm
Does it give you the correct result when you run your code? That's what matters, right?

Is it possible for a user to not have a first name (empty name)? If so, you need to account for this.
Otherwise, it looks OK.

(Also, while it's not wrong, there's really no purpose for that function being part of a class. It could just be a standalone function.)
Last edited on Oct 26, 2020 at 4:05pm
Oct 26, 2020 at 4:11pm
you can just get the letter.
fname[0] is the first letter. substr does way, way too much work for the same result.

OSO design (objects for the sake of having objects, even when pointless) is to be avoided in C++. So echo above, no class needed if the class does not have anything in it but one function.

you may also want
fname+" " + lname; //a space between may be in your needs?
Oct 26, 2020 at 5:12pm
1
2
3
4
5
6
7
8
9
10
string Converter::generateUsername(string fname, string lname)
{
string username;
fname = fname.substr( 0, 1); // get the first letter in fname

username = fname + lname; // joing fname and lname string to form username

return username;

} 


would this be correct?


'works' - yes. Correct - no.

fname, lname are passed by value rather than by ref - so a copy is undertaken. There is no need to have a separate variable username. Also jonnin comments.

To check that first isn't empty:

1
2
3
4
std::string generateUsername(const std::string& first, const std::string& last)
{
	return first.empty() ? last : first[0] + last;
}


Last edited on Oct 26, 2020 at 5:13pm
Topic archived. No new replies allowed.