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.
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.
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
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?
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.