1

human header

class human { char name[]; public: void setName(char nameValue[]); char* getName(); } 

human cpp

void human::setName(char nameValue[]) { char name[] = "walter"; } char* human::getName() { return name; } 

main cpp

int main() { human myHuman; char name[] = "walter"; myHuman.setName(name); char* result3 = myHuman.getName(); cout << "My human has a name of " << result3 << endl; return 0; } 

I assign the string "walter" but when I print it I get "╠╠╠╠╦ß╩d└²╒".

I don't understand which part is wrong. Can you tell me what's wrong with my code?

11
  • Please use std::string Commented Feb 27, 2021 at 21:53
  • i cannot. it is homework. pls help me with char Commented Feb 27, 2021 at 21:54
  • There are many errors in your program. The worst one is using char* instead of std::string. Commented Feb 27, 2021 at 21:54
  • 1
    setName implementation is confusing to me. You are assigning a constant name to the new char name [] variable name. 1) change the char name [] definition in header file to char * name. 2) assign nameValue (input of setName to same "name" variable). DO not redefine a new char name[] inside setName(). Commented Feb 27, 2021 at 22:19
  • 1
    @bkk With that change only, your class will point at an external character array. If that array goes out of scope and is destroyed, the pointer in your class will be dangling. Dereferencing it will cause undefined behavior. Commented Feb 27, 2021 at 22:25

1 Answer 1

3

First and foremost you are assigning nameValue to a variable local to the function setName, which means that the class variable name is still unitialized when you return it in getName, hence the strange output.

The char name[]; declaration is also incorrect, C++ does not allow for variable length arrays or arrays with unspecified bounds unless they are immediately initialized in which case the size will be deduced given the size of the assigned string.

On that note, warnings must have been issued by your compiler, if not, crank them up, or better yet, make it treat warnings as errors, you'll have more robust code.

For the purpose of your assignment you should just go ahead and use pointers, a small problem arises because C++ does not allow for assignment of string literals to char* variables, this is easily fixed if you use const char*, your compiler may allow the former but it is illegal as per C++ rules.

Applying the corrections above, your code should look more like this:

class human { const char* name; public: void setName(const char *nameValue); const char* getName(); }; void human::setName(const char *nameValue) { name = nameValue; } const char* human::getName(){ return name; } int main() { human myHuman; const char* name = "walter"; myHuman.setName(name); const char* result3 = myHuman.getName(); cout << "My human has a name of " << result3 << endl; return EXIT_SUCCESS; } 
Sign up to request clarification or add additional context in comments.

1 Comment

it works finally and i am for sure because i changed the name and still output give the recent one. thank you so much and now i just look at your code to understand to logic. you saved my homework

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.