What is the flaw in this C++ code? -
(I'm starting for C ++. But I'm familiar with some other programs, especially with Java. )
Can anyone help me find fault in this C ++ code?
string & amp; GetFullName (string name, bull male) {string fullName = name; If (male) {full name = string (â ???? sree â ????) + full name; Return full name; }}
There are at least three issues:
< Li> First of all, you are returning to the context of the local variable.
-
Second, there is no return when male is false . I mean, what would happen if if block was not executed? What will be your function? There is no return statement for this matter. - Third,
is not literally a word of "Mr. a word" string "Mr." Note the difference is a string literal .
It can be fixed:
string getFullName (string name, bull male) {if (male) {return string ( "Mr.") + name;} and {return "Ms." + name; // no need to type 'string' (MS))}} local variables There is no need for
or simply:
string getFullName (string name, bool Male} {return (male? String ("mr"): string ("ms")) + name;}
Comments
Post a Comment